typescript - Typescript - 从类注释中访问类字段
问题描述
使用以下代码:
@Component({
props: {
value: String
},
mounted() {
//Do something with `bar`
this.bar = this.bar + " is now mounted";
}
})
export default class Foo extends Vue {
private bar : string = "This element";
}
您在打字稿控制台中收到以下错误,但代码运行正常。
37:14 Property 'bar' does not exist on type 'Vue'.
05 | mounted() {
06 | //Do something with `bar`
07 | this.bar = this.bar + " is now mounted";
| ^
08 | }
09 | })
解决方案
您需要为类Component
注释提供类型参数,以便编译器知道它this.bar
确实存在。
@Component<Foo>({
props: {
value: String
},
mounted() {
//Do something with `bar`
this.bar = this.bar + " is now mounted";
}
})
export default class Foo extends Vue {
private bar : string = "This element";
}
此代码编译没有错误。
推荐阅读
- python - 使用 Systemctl 找不到 Python
- python-3.x - 为什么有些表情符号没有转换回它们的表示形式?
- python - 类型错误:“str”和“float”的实例之间不支持“>”
- node.js - 如何在 URL 中没有端口号的情况下使用 Traefik + Docker + Nodejs 访问我的 API?
- sql-server - 500 RequestError:无效的列名“字符串”和环回 4
- php - 将带有循环的 HTML 传递给 php 中的变量
- ios - 我无法在 SwiftUI 的动态 ListView 中加载我的数据
- bash - 使用文件的输出作为 bash 脚本的新变量
- javascript - Javascript - 替换非舍入数字的函数错误
- c++ - 为什么不能破坏一个对象,然后用相同的名称再次实例化它