亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Stencil JS - 使用 Prop 值啟動 State 變量?

Stencil JS - 使用 Prop 值啟動 State 變量?

斯蒂芬大帝 2023-07-06 18:22:38
在我的 Stencil 組件中,我想使用傳遞下來的 prop 值來啟動我的狀態變量。如何在模板中做到這一點?我嘗試將值分配給 componentWillLoad 中的狀態變量,但它不起作用。@Prop() passedVal @State() someVal = ??我是 Stencil 的新手,而且我來自 VueJS,所以請忍受我看似幼稚的問題。
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

最好觀察 prop 的變化,然后更新狀態。


@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因為觀察只會在組件加載后開始。


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號