Typescript和Vue.js-属性在初始化之前使用
这是我的密码Typescript和Vue.js-属性在初始化之前使用,typescript,vue.js,Typescript,Vue.js,这是我的密码 export default class PrimitiveLink extends Vue { style = { // Reset display: 'inline-block', textDecoration: 'none', outline: 'none', // Theme ...this.themeStyle, }; @Prop(String) readonly href!: string @Prop(S
export default class PrimitiveLink extends Vue {
style = {
// Reset
display: 'inline-block',
textDecoration: 'none',
outline: 'none',
// Theme
...this.themeStyle,
};
@Prop(String) readonly href!: string
@Prop(String) readonly title: string | undefined
@Prop(Object) readonly themeStyle!: object
}
我收到TypeScript错误属性“themeStyle”在初始化之前被使用。
这意味着什么?我应该如何修复它?感谢您提供的所有答案。这意味着您在初始化之前使用了
主题样式
基本上是这样的:
var a=themeStyle;
var themeStyle=“某物”;
解决方案是在使用前定义主题样式
或者,您可以使用…this['themeStyle']
这是TypeScript所允许的,但前提是您不能“正确”解决它。类属性/字段是构造函数主体代码的语法糖,并按照定义的相同顺序进行计算:
constructor() {
this.style = {
/* ... */
...this.themeStyle, // not defined
};
this.themeStyle /* = ... */;
}
此顺序也适用于未初始化且在运行时不存在的TypeScript属性<代码>主题样式
应放在样式
之前
此代码段的问题在于它不符合Vue生命周期。在创建类实例时,属性值不可用,themeStyle
应该是未定义的
样式
应为计算属性,以对最大样式
更改作出反应:
get style() {
return {
// Reset
display: 'inline-block',
textDecoration: 'none',
outline: 'none',
// Theme
...this.themeStyle,
};
}
或在主题样式可用时分配:
style!: object;
mounted() {
this.style = {
display: 'inline-block',
textDecoration: 'none',
outline: 'none',
...this.themeStyle,
};
}
谢谢你的充分解释。我喜欢使用计算方法。我使用了道具的默认值,但仍然得到了这个错误@Prop({default:()=>“”})主体;httpBody:string=this.body错误:“属性“body”在初始化之前使用。Vetur(2729)”