nativescript-masked-text-field icon indicating copy to clipboard operation
nativescript-masked-text-field copied to clipboard

Usage with NativeScript-Vue

Open sougiovn opened this issue 7 years ago β€’ 26 comments

I'm new to NativeScript and I'm starting with Vue, how do I use it with NativeScript-Vue?

I tested like this, but got no result

<template>
  <Page 
    xmlns="http://schemas.nativescript.org/tns.xsd" 
    xmlns:mtf="nativescript-masked-text-field">
    <mtf:MaskedTextField v-model="cpf" />
  </Page>
</template>

sougiovn avatar Nov 08 '18 22:11 sougiovn

Hey @gigioSouza , sadly I'm not a Vue developer so i cannot help you on this. I think best to ask on SO so you can get more audience and an appropriate answer.

PeterStaev avatar Nov 09 '18 12:11 PeterStaev

Hi,

you need to register the component in your main.js file

Vue.registerElement('MaskedTextField',
    () => require('nativescript-masked-text-field').MaskedTextField
);

then, use in your vue component

<MaskedTextField
    v-model="cpf"
    mask="999.999.999-99"
    keyboardType="phone"
/>

gab0gomes avatar Nov 14 '18 16:11 gab0gomes

@Gabrielr2508 It "worked", the app compiled and I'm able to use the MaskedTextField tag/component, but the mask isn't applied.

sougiovn avatar Nov 14 '18 21:11 sougiovn

@gigioSouza The mask works here, but v-model doesn't. Can't figure out why not. :(

gab0gomes avatar Nov 14 '18 21:11 gab0gomes

@gigioSouza The mask works here, but v-model doesn't. Can't figure out why not. :(

I tried to bind the "text" property of the field and update it in the textchange event, but this caused me to not be able to change the text in the emulator

gab0gomes avatar Nov 14 '18 22:11 gab0gomes

I was able to get the value using a refs property and in function it retrieves the value this way $ refs.refName.nativeView.text

lennonphosfato avatar Jan 23 '19 18:01 lennonphosfato

Hey When I try to use this component in my NativeScript Vue app I get the error

JS ERROR TypeError: Could not load view for: maskedtextfield. TypeError: view_1.CSSType is not a function. (In 'view_1.CSSType("MaskedTextField")', 'view_1.CSSType' is undefined)

I have registered the component using

Vue.registerElement('MaskedTextField',
    () => require('nativescript-masked-text-field').MaskedTextField
);

Then I just add it to the page and when I do I get the error.

<MaskedTextField
                                        v-model="email"
                                        mask="999.999.999-99"
                                        keyboardType="phone"
                                />

I am super new to Vue and Nativescript som I have no idea what I do wrong.

kfvit-andreaskviby avatar Feb 22 '19 12:02 kfvit-andreaskviby

@andreaskvibydialect , make sure you are using Nativescript 4 or later. The CSSType was added relatively recent πŸ˜ƒ

PeterStaev avatar Feb 25 '19 09:02 PeterStaev

@andreaskvibydialect , make sure you are using Nativescript 4 or later. The CSSType was added relatively recent πŸ˜ƒ

Hey Sorry for being a novice but do you mean tns-core-modules?

kfvit-andreaskviby avatar Feb 25 '19 12:02 kfvit-andreaskviby

@andreaskvibydialect , make sure you are using Nativescript 4 or later. The CSSType was added relatively recent πŸ˜ƒ

Hey Sorry for being a novice but do you mean tns-core-modules?

That is correct.

PeterStaev avatar Feb 25 '19 12:02 PeterStaev

@andreaskvibydialect , make sure you are using Nativescript 4 or later. The CSSType was added relatively recent πŸ˜ƒ

Hey Sorry for being a novice but do you mean tns-core-modules?

That is correct.

Hey I have version 5.2.0 when I run tns --version now and still I can't make it work in my app. Any other ideas of my registration is wrong of if it has to be locally referenced or in some way.

kfvit-andreaskviby avatar Feb 25 '19 12:02 kfvit-andreaskviby

I have version 5.2.0 when I run tns --version now and still I can't make it work in my app. Any other ideas of my registration is wrong of if it has to be locally referenced or in some way.

Sadly I'm not a Vue developer so can't help much with the registration and other Vue stuff...

PeterStaev avatar Feb 25 '19 12:02 PeterStaev

I have version 5.2.0 when I run tns --version now and still I can't make it work in my app. Any other ideas of my registration is wrong of if it has to be locally referenced or in some way.

Sadly I'm not a Vue developer so can't help much with the registration and other Vue stuff...

I understand, me neither but if you hear or see some NativeScript-Vue App that makes it possible to get this component to work please let me know.

kfvit-andreaskviby avatar Feb 25 '19 12:02 kfvit-andreaskviby

Hey When I try to use this component in my NativeScript Vue app I get the error

JS ERROR TypeError: Could not load view for: maskedtextfield. TypeError: view_1.CSSType is not a function. (In 'view_1.CSSType("MaskedTextField")', 'view_1.CSSType' is undefined)

I have registered the component using

Vue.registerElement('MaskedTextField',
    () => require('nativescript-masked-text-field').MaskedTextField
);

Then I just add it to the page and when I do I get the error.

<MaskedTextField
                                        v-model="email"
                                        mask="999.999.999-99"
                                        keyboardType="phone"
                                />

I am super new to Vue and Nativescript som I have no idea what I do wrong.

Try the following: Remove the "platforms" folder and start "tns run ..." again.

I just tried the package and it worked.

prinzt avatar Feb 25 '19 15:02 prinzt

I have version 5.2.0 when I run tns --version now and still I can't make it work in my app. Any other ideas of my registration is wrong of if it has to be locally referenced or in some way.

Sadly I'm not a Vue developer so can't help much with the registration and other Vue stuff...

I understand, me neither but if you hear or see some NativeScript-Vue App that makes it possible to get this component to work please let me know.

hei i got this plugin working on vue nativescript

this on my main.js Vue.registerElement('MaskedTextField', () => require('nativescript-masked-text-field').MaskedTextField)

this on my component page <MaskedTextField mask="999.999.999" keyboardType="phone" />

and who still use ns-vue 1.3.1 version, u should to edit your package.json "tns-core-modules": "~4.0.0", // i changed to v 4.0.0, previously its 3.*.*

then run npm run clean do npm install --save

finally npm run watch:android or ios and it should work

Screen Shot 2019-05-16 at 11 42 38

hope that helps :)

budi7 avatar May 16 '19 04:05 budi7

v-model doesn't seem to work, anyone with same behaviour and how to fix it?

seergiue avatar May 28 '19 18:05 seergiue

The textfield is not even showing up for me (tns 5.3.4)...

Jonarod avatar May 31 '19 07:05 Jonarod

Same for me, it does not show up

FrΓ₯n: Jonarod [email protected] Svara till: PeterStaev/nativescript-masked-text-field [email protected] Datum: fredag 31 maj 2019 09:35 Till: PeterStaev/nativescript-masked-text-field [email protected] Kopia: Andreas Kviby [email protected], Mention [email protected] Γ„mne: Re: [PeterStaev/nativescript-masked-text-field] Usage with NativeScript-Vue (#29)

The textfield is not even showing up for me (tns 5.3.4)...

β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PeterStaev/nativescript-masked-text-field/issues/29?email_source=notifications&email_token=ALIN32VOAW7NSTIXGTXX3STPYDIKTA5CNFSM4GCWOX62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWUOL3Q#issuecomment-497608174, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALIN32SJ3DF7QS3MBU6LUB3PYDIKTANCNFSM4GCWOX6Q.

kfvit-andreaskviby avatar May 31 '19 07:05 kfvit-andreaskviby

In order for v-model to work, you will need to specify which property/event to use, like this:

Vue.registerElement(
    'MaskedTextField',
    () => require('nativescript-masked-text-field').MaskedTextField,
    {
        model: {
            prop: 'text',
            event: 'textChange'
        }
    }
);

Check this example:

https://play.nativescript.org/?template=play-vue&id=3Rac7h&v=3

bundyo avatar May 31 '19 11:05 bundyo

@bundyo helped me out from th slack forum. Now it works. However, I found the origin of the plugin not showing was because I was actually registering the component twice: once like suggested by @bundyo :

Vue.registerElement(
    'MaskedTextField',
    () => require('nativescript-masked-text-field').MaskedTextField,
    {
        model: {
            prop: 'text',
            event: 'textChange'
        }
    }
);

but then also in my component.vue: DON'T DO THIS

<template>
    <MaskedTextField mask="99/99" v-model="expiryDate" />
</template>

<script>
import {MaskedTextField} from "nativescript-masked-text-field"  // REMOVE THIS

export default {
    components: {MaskedTextField}, // REMOVE THIS
    data(){
        return {
            expiryDate: ''
        }
    }
}
</script>

Jonarod avatar May 31 '19 12:05 Jonarod

@Jonarod Thanks that helped once again.

MohammedBashiru avatar May 31 '19 12:05 MohammedBashiru

Even with @bundyo answer it is still not working for me. Binding text property doesn't return anything in data. When I use with v-model it binds a whole object (not just the value) but I can't type in the field (tried with both virtual and physical keyboard).

andresilva-cc avatar Aug 26 '19 02:08 andresilva-cc

How to add v-model to this.

Nayeb-karimi avatar Sep 24 '19 05:09 Nayeb-karimi

I ended up using ref and obtaining the value through this.$refs.refName.nativeView.text, as suggested by @lennonphosfato

andresilva-cc avatar Oct 03 '19 19:10 andresilva-cc

I ended up using the following code snippet to workaround the v-model issue:

<MaskedTextField
      mask="(999) 999-9999"
      keyboardType="phone"
      ref="maskedtextfield"
      @textChange="textFieldChange($event)"
    />

data() {
    return {
        val: null
    }
},
methods:{
    textFieldChange(args) {
      this.val = args.value;
    }
}

Hopefully this helps!

keithgulbro avatar Feb 04 '20 20:02 keithgulbro

Hi,

I am getting this error when trying to load the app with MaskedTextField. Did the same as suggested by @bundyo.

TypeError: Class constructor TextField cannot be invoked without 'new'

Dartow avatar Jan 02 '21 18:01 Dartow