Property 'XXX' does not exist on type 'CombinedVueInstance>>'

后端 未结 4 2041
醉话见心
醉话见心 2021-01-18 04:27

I created a vue component with TypeScript, and I\'m getting this error in data() and in methods():

Property \'xxx\' does not exist          


        
相关标签:
4条回答
  • 2021-01-18 04:51

    This seems to be inexplicably caused by using this.$store to compute the return value of profilePath, combined with the unspecified return type on its declaration.

    One workaround is to specify the return type as string:

    profilePath: function(): string {
    

    verified with npm run serve and npm run build, using Vue CLI 3.7.0 on macOS Mojave

    GitHub demo

    0 讨论(0)
  • 2021-01-18 04:57

    As mentioned in the Typescript Support section of the Vue documentation:

    Because of the circular nature of Vue’s declaration files, TypeScript may have difficulties inferring the types of certain methods. For this reason, you may need to annotate the return type on methods like render and those in computed.

    In your case, you should change profilePath: function () { to profilePath: function (): string {

    You might come across the same error if you have a render() method that returns a value, without a : VNode annotation.

    0 讨论(0)
  • 2021-01-18 04:58

    Same issue is described here https://www.gitmemory.com/issue/vuejs/vue/9873/485481541

    As its a problem with TS 3.4.x, 3.9.6 works very well for me now

    0 讨论(0)
  • 2021-01-18 05:00

    You need to use function in the correct way to preserve the reference of this.

    methods: {
                toggle() {
                    this.open = !this.open
                    if (this.open) {
                        // Add click listener to whole page to close dropdown
                        document.addEventListener('click', this.close)
                    }
                },
                close() {
                    this.open = false;
                    document.removeEventListener('click', this.close)
                }
            }
    
    0 讨论(0)
提交回复
热议问题