【问题标题】:How to update props in Child component?如何更新子组件中的道具?
【发布时间】:2019-06-06 03:07:31
【问题描述】:

当父组件的计算数据发生变化时,子组件仍然使用旧的道具,该道具来自父组件的计算数据。

【问题讨论】:

  • 请提供一些代码来证明您的问题。
  • 我只能假设您在创建过程中将道具值分配给数据值,即{ dataValue: this.propValue },并且您希望dataValue 更新。不是这种情况。您应该始终能够依赖要更新的道具值

标签: vue.js


【解决方案1】:

我个人为此使用手表。

Vue.component(
    data: function(){
        return{
            someProp:initProp
        }
    },
    ....
    props:['initProp'],
    ...
    watch:{
        initProp:function(val){
            someProp = val;
        }
    }
);

像魅力一样工作。

【讨论】:

    【解决方案2】:

    您可以在创建子组件时使用ref="xxx",然后通过父组件的this.refs.xxx.prop = "some value" 设置它。

    示例如下:

    <child ref="xxx" v-bind:prop1="10">
    </child>
    
    this.refs.xxx.prop1 = 15
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 1970-01-01
      • 2019-07-08
      • 2020-08-07
      • 2019-11-17
      • 2021-08-09
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      相关资源
      最近更新 更多