问题描述
我在 Vue 类组件中使用 props.props 在构造函数中定义,没有值.这可以编译并且工作正常,但是自从最新的 VS Code/TSLint 更新以来,我收到以下警告:
I am using props in Vue Class components. The props are defined in the constructor without a value. This compiles and works fine, but since the latest VS Code / TSLint update, I get the following warning:
属性 'tag' 没有初始化器,也没有在构造函数中明确赋值.
Vue 类组件
export default class Browser extends Vue {
@Prop({default: '', required:false})
tag: string
created(){
console.log("the tag prop is " + this.tag)
}
}
如果我确实分配了它,我会收到 Vue 警告,提示您不应在子组件中操作 Prop.
If I DO assign it, I get the Vue warning that you shouldn't manipulate a Prop in a child component.
[Vue 警告]:避免直接改变 prop,因为每当父组件重新渲染时,该值将被覆盖
由于这似乎主要是一个 linting 问题,有没有办法禁用它?还是我的代码真的错了?
Since this seems primarily a linting problem, is there a way to disable this? Or is my code actually wrong?
推荐答案
更新 2020/06/15
我当时给出的原始答案是一种间歇性的解决方案,而不是正确的解决方案.这个答案是正确的做法;用 !
附加道具名称.
Update 2020/06/15
My original answer I gave at the time was a intermittent solution and not the correct one. This answer is the correct way of doing this; Appending the prop name with a !
.
我遇到了同样的问题.我通过将 "strictPropertyInitialization": false,
添加到 tsconfig.json 的 compilerOptions 来修复它.
I had the same issue. I fixed it by adding "strictPropertyInitialization": false,
to tsconfig.json's compilerOptions.
{
"compilerOptions": {
"outDir": "./built/",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"experimentalDecorators": true,
"module": "es2015",
"moduleResolution": "node",
"target": "es5",
"strictPropertyInitialization": false,
"lib": [
"es5",
"es2015",
"dom",
"ScriptHost"
]
},
"include": [
"./src/**/*"
]
}
这篇关于Vue Prop 没有初始化器,也没有在构造器中明确赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!