Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"[Vue warn]: Error in data(): "Error"" - when using typescript components #263

Closed
sebasijan opened this issue Aug 12, 2021 · 1 comment
Closed

Comments

@sebasijan
Copy link

sebasijan commented Aug 12, 2021

Following the guide, but adjusing for vue 2 and typescript, I have the below setup, and it compiles fine but when I go to the page I see the 3 errors at the bottom.

After copy-pasting the guide example exactly it seems to work fine, but I'm wondering if it's possible to get it working in the below way (using Component, Prop, ts etc) ?

<template>
    <input ref="inputRef"
           :value="formattedValue"
           type="text" >
</template>

<script lang="ts">
    import { Vue, Component, Prop } from "vue-property-decorator";
    import useCurrencyInput from 'vue-currency-input'

    @Component({
        name: "CurrencyInput",
        components: {}
    })
    export default class CurrencyInput extends Vue {
        @Prop() value!: number;
        @Prop() options!: any;

        public formattedValue: string | null = null;
        public inputRef: any = null;

        constructor() {
            super();

            const { formattedValue, inputRef } = useCurrencyInput(this.options)

            this.inputRef = inputRef;
            this.formattedValue = formattedValue.value;
        }
    }
</script>

Calling normally like this:

<currency-input v-model="testValue" options="{ currency: 'EUR' }" />

Errors:

[Vue warn]: Error in data(): "Error"
found in
---> at wwwroot/components/util/currencyInput.vue

vue.js:1896 Error
at useCurrencyInput (index.esm.js:475)
at new CurrencyInput (currencyInput.vue:62)
at collectDataFromConstructor (vue-class-component.esm.js:168)
at VueComponent.data (vue-class-component.esm.js:230)
at getData (vue.js:4744)
at initData (vue.js:4701)
at initState (vue.js:4640)
at VueComponent.Vue._init (vue.js:4999)
at new CurrencyInput (vue.js:5146)
at createComponentInstanceForVnode (vue.js:3289)

vue.js:633 [Vue warn]: Property or method "formattedValue" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
found in
---> at wwwroot/components/util/currencyInput.vue

@sebasijan sebasijan changed the title [Vue warn]: Error in data(): "Error" "[Vue warn]: Error in data(): "Error"" - when using typescript components Aug 12, 2021
@dm4t2
Copy link
Owner

dm4t2 commented Aug 12, 2021

It seems the setup method needs to be a @Component option. See vuejs/composition-api#136

@Component({
  setup(props, context) {
    //...
  }
})
export default class CurrencyInput extends Vue {
  //...
}

@dm4t2 dm4t2 closed this as completed Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants