在我的模板组件中,我想用传递的螺旋桨值启动我的状态变量。如何在模版中做到这一点?
我尝试在componentWillLoad中将值赋值给状态变量,但它不起作用。
@Prop() passedVal
@State() someVal = ??我是新来的模版,我来自VueJS,所以请容忍我看似不切实际的问题。
发布于 2020-11-03 21:45:06
最好是观察变化的道具,然后更新状态。
@Component({ tag: 'my-comp' })
export class MyComp {
@Prop() foo: string;
@State() bar: string;
@Watch('foo')
onFooChange() {
this.bar = this.foo;
}
componentWillLoad() {
this.onFooChange();
}
render() {
return this.foo + this.bar;
}
}您可以在componentWillLoad中调用watcher方法,因为只有在组件加载之后才会开始监视。
发布于 2022-01-30 08:30:58
对于未来的到来者,@Watch以@Prop变量的名称作为参数进行监视。每当该支柱的值发生变化时,由@Watch修饰的函数将以newValue和oldValue作为参数进行调用。
export class SomeClass{
@Prop() isValid: boolean = true;
@Watch("isValid")
watchHandler(newValue, oldValue) {
console.log("newValue: ", newValue, "oldValue: ", oldValue);
this.isValid = newValue;
}
}https://stackoverflow.com/questions/64649424
复制相似问题