javascript - Stencil JS - 使用 Prop 值启动状态变量?
问题描述
在我的 Stencil 组件中,我想使用向下传递的 prop 值启动我的状态变量。如何在 Stencil 中做到这一点?
我尝试将值分配给 componentWillLoad 中的状态变量,但它不起作用。
@Prop() passedVal
@State() someVal = ??
我是 Stencil 的新手,我来自 VueJS,所以请忍受我这个看似无聊的问题。
解决方案
最好观察道具的变化,然后更新状态。
@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
因为观察只会在组件加载后开始。
推荐阅读
- azure-devops - 如何从同一管道中的不同作业下载管道工件
- python - 张量流导入错误:无法从“tensorflow.python.util”导入名称“module_wrapper”
- php - 如何减去数组的值但仍在位置
- postman - 邮递员集合中的所有请求都不会运行
- reactjs - Axios vs context.spHttpClient:何时在 SPFX http 请求中使用哪个?
- angular - 角度中的数组对象始终默认为0,同时从中间删除一个对象并添加一个新对象
- android - 如何标记 android studio 中的输入文本以在 NLP 模型中处理?
- android - 带有 ViewPager 的 Butterknife NullPointerException
- c# - 使用 SQL Server 表中的列填充 CartesianChart 标签
- hibernate - 将缓存与 Spring Boot 集成