问题描述
我用TypeScript创建了一个vue组件,并且在 data()
和 methods()
中得到了这个错误:
I created a vue component with TypeScript, and I'm getting this error in data()
and in methods()
:
Property 'xxx' does not exist on type 'CombinedVueInstance<Vue, {},
{}, {}, Readonly<Record<never, any>>>'.
例如:
33:18 Property 'open' does not exist on type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>'.
31 | methods: {
32 | toggle: function () {
> 33 | this.open = !this.open
| ^
34 | if (this.open) {
35 | // Add click listener to whole page to close dropdown
36 | document.addEventListener('click', this.close)
使用 this.close()
的任何时间也会显示此错误.
This error also shows any time this.close()
is used.
这是组件:
<script lang='ts'>
import Vue from 'vue';
import axios from 'axios'
export default Vue.extend({
data: function () {
return {
open: false
}
},
computed: {
profilePath: function () {
return "/user/" + this.$store.state.profile.profile.user.id
}
},
methods: {
toggle: function () {
this.open = !this.open
if (this.open) {
// Add click listener to whole page to close dropdown
document.addEventListener('click', this.close)
}
},
close: function () {
this.open = false;
document.removeEventListener('click', this.close)
}
}
})
</script>
是什么导致此错误?它似乎仍在开发中并带有错误,但是当我部署到生产环境时,它们会引起问题.
What is causing this error? It seems to still build in development with the errors, but they are causing issues when I deploy to production.
推荐答案
如打字稿支持部分:
在您的情况下,应将 profilePath:function(){
更改为 profilePath:function():string {
In your case, you should change profilePath: function () {
to profilePath: function (): string {
如果具有不带:VNode
批注的返回值的render()方法,则可能会遇到相同的错误.
You might come across the same error if you have a render() method that returns a value, without a : VNode
annotation.
这篇关于属性'XXX'在类型'CombinedVueInstance< Vue,{},{},{},Readonly< Record< never,any>>'上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!