HarmonyOS 鸿蒙Next关于@watch监听只回调一次的问题
HarmonyOS 鸿蒙Next关于@watch监听只回调一次的问题
我在尝试编写一个自定义的组件,其中使用到了@watch监听属性变化的方式
子组件代码如下:
@Prop @Watch(‘onOrderUpdated’) verifyOrder: string
onOrderUpdated(propName: string): void {
if (this.verifyOrder == ‘clear’) {
this.clearPassword()
}
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder == <span class="hljs-string"><span class="hljs-string">'show'</span></span>) {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.showKeyboard()
}
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder == <span class="hljs-string"><span class="hljs-string">'hide'</span></span>) {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.hideKeyboard()
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
父组件调用代码如下:
@State verifiOrder: string = ‘’
Stack() {
ETVerificationCode({
verifyOrder: this.verifiOrder
})
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
通过一个点击事件触发父控件verifyOrder属性的改变,现在发现仅在第一次点击时子控件会走监听方法,第二次及以后监听方法就完全不走了。
是我代码逻辑的问题么?各位大神有没有遇到类似问题的,求帮助。
关于HarmonyOS 鸿蒙Next关于@watch监听只回调一次的问题的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
[@State](/user/State) verifyOrder: string = '';
build() {
Row(){
MyComponent({
verifyOrder:this.verifyOrder
})
.width(200)
Button(<span class="hljs-string"><span class="hljs-string">"清空"</span></span>).onClick(()=>{
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder=<span class="hljs-string"><span class="hljs-string">"clear"</span></span>
})
Button(<span class="hljs-string"><span class="hljs-string">"显示"</span></span>).onClick(()=>{
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder=<span class="hljs-string"><span class="hljs-string">"clear"</span></span>
})
Button(<span class="hljs-string"><span class="hljs-string">"隐藏"</span></span>).onClick(()=>{
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder=<span class="hljs-string"><span class="hljs-string">"hide"</span></span>
})
}
}
}
@Component
struct MyComponent{
@Prop @Watch(‘onOrderUpdated’) verifyOrder: string
@State password:string=""
onOrderUpdated(propName: string): void {
console.log(this.verifyOrder);
if (this.verifyOrder == ‘clear’) {
this.clearPassword()
}
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder == <span class="hljs-string"><span class="hljs-string">'show'</span></span>) {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.showKeyboard()
}
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.verifyOrder == <span class="hljs-string"><span class="hljs-string">'hide'</span></span>) {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.hideKeyboard()
}
}
hideKeyboard() {
}
showKeyboard() {
}
clearPassword(){
this.password=""
}
build() {
TextInput({text:this.password})
.type(InputType.Password)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
要每次回调,目前有个方法可以使用systemDateTime.getTime()作为其中一个参数进行传递