- 49
- 0
问题描述
Property 'activeName' does not exist on type 'Vue'
问题出现的环境背景及自己尝试过哪些方法
vue-property-decorator
在beforeRouteEnter中给属性初始化时,提示Property 'activeName' does not exist on type 'Vue'
相关代码
<script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator";
@Component({
components: {},
beforeRouteEnter(to:any, from: any, next){
next(vm => {
console.log('vm-->', vm) //打印vm中存在activeName这个属性
vm.activeName = 'date'
// 此处提示:Property 'activeName' does not exist on type 'Vue'
})
}
})
export default class Demo extends Vue {
activeName:string = 'date'
}
</script>
你期待的结果是什么?实际看到的错误信息又是什么?
1、在beforeRouteEnter中打印 vm这个对象,是存在 activeName这个属性的
2、但是在使用 vm.activeName = 'date' 进行初始化时,提示:Property 'activeName' does not exist on type 'Vue'
尝试过的方法:
在my-property.d.ts文件中,将需要初始化的属性进行全局注册。但是这个方法,如果我很多文件需要使用不同的属性,那我就需要在这个文件中,每次需要使用到哪个属性,都进行全局注册一次,显得麻烦。各位大神有没有其他的解决办法?
// 1. 确保在声明补充的类型之前导入 'vue'
import Vue from 'vue'
// 2. 定制一个文件,设置你想要补充的类型
// 在 types/vue.d.ts 里 Vue 有构造函数类型
declare module 'vue/types/vue' {
// 3. 声明为 Vue 补充的东西
interface Vue {
$msg: Function,
$bus:Vue,
activeName: string
}
} - 共 0 条
- 全部回答
-
转圈的旅人 普通会员 1楼
The error message "Property 'activeName' does not exist on type 'Vue'" is indicating that the variable "activeName" is of type "Vue" but the function or expression you are trying to call on it is not expecting it to have that type. Here's a breakdown of what's happening:
- The error is occurring because the
activeNamevariable is defined as a property of the type "Vue" in the following line:javascript const activeName = this.activeName;
The
thiskeyword is used to refer to the Vue instance in this code. When you try to access theactiveNameproperty on this instance, you're accessing the internalactiveNameproperty of the Vue instance, which is a string literal (i.e., "activeName").-
However, the function or expression you are trying to call on
activeNameis trying to use it as a Vue object property, which is not possible when theactiveNamevariable is of type "Vue". When a variable is of type "Vue", it refers to a singleton object that contains a single instance of the Vue class. This object has properties and methods that are specific to Vue, such as theactiveNameproperty in this case. -
To fix this error, you need to make sure that the
activeNamevariable is not a Vue object property and that it refers to a string literal. Here's an example of how you can do this:
```javascript const activeName = 'activeName';
// Example usage: call the 'activeName' property directly const name = activeName; console.log(name); // Output: activeName
// Call the 'activeName' property as a string literal const name = 'activeName'; console.log(name); // Output: activeName ```
In the example above, we initialize the
activeNamevariable as a string literal"activeName", and then we call theactiveNameproperty directly without using thethiskeyword or as a Vue object property. This allows us to access and use theactiveNameproperty correctly in the function or expression.By making sure that
activeNameis not a Vue object property, the error should be resolved. - The error is occurring because the
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
