void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property b

简介: void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: vue项目示例,请参考甄佰 单向数据流所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。

void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated:

单向数据流
所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。这样会防止从子组件意外改变父级组件的状态,从而导致你的应用的数据流向难以理解。

1560217648343
vue官网截图

vue报错常见场景,以下动态tab切换,子组件分页需要改变初始传参的正确写法

在父组件引入子组件

    <div class="m-tab">
      <div class="m-navbar">
        <a v-for="item in tablist" class="m-items" :class="{ mactive : activetab == item.idx }" @click="tabs(item.idx)" :key="item.idx">
          {{ item.title }}
          <i class="m-sign"></i>
        </a>
      </div>
      <div v-for="item in tablist" class="m-sub" v-show="activetab == item.idx">
        <insuresub :post="item"></insuresub>
      </div>
    </div>
    
  import navbar from '@/components/navbar'
  import insuresub from '@/components/insuresub'


  export default {
    name: "insure",
    components: {
      navbar,
      insuresub,
    },
    data() {
      return {
        activetab: 'c1',
        tablist: [{
          idx: 'c1',
          title: '医疗险',
          page: '2',
          count: '12',
          sub:[{
            price: '100'
          },{
            price: '80'
          }]
        },{
          idx: 'c2',
          title: '重疾险',
          page: '5',
          count: '8',
          sub:[{
            price: '70'
          },{
            price: '90'
          }]
        },{
          idx: 'c3',
          title: '寿险',
          page: '3',
          count: '11',
          sub:[{
            price: '200'
          },{
            price: '800'
          }]
        }],
      }
    },
    methods: {
      tabs (e){
        this.activetab = e;
      }
    }
  }

下面是子组件接收传参动态改变,子组件 next() 需要动态请求数据达到分页效果,我这里写的静态数据方便理解

{{ post.idx }}-{{ post.page }}页/{{ post.count }}页
    <a v-for="item in list.sub" class="card" @click="next(list.page)">
      <div class="cardhead">
        <div class="cardbox">
          <div class="color-m size-a">1623人正在保障中...</div>
          <div class="cardname">产品计划</div>
          <div class="color-c size-a">可多次赔付 | 340种疾病 | 保障全面</div>
          <span class="color-l">¥<span class="size-e">{{ item.price }}</span></span>起
        </div>
        <img class="cardimg" src="../assets/images/index4.jpg" alt="图片加载失败" />
      </div>
      <div class="cardbody">
        <div>
          <div class="size-c">200万</div>
          <span class="color-c size-a">保额</span>
        </div>
        <div>
          <div class="size-c">1周岁-60周岁</div>
          <span class="color-c size-a">投保年龄</span>
        </div>
        <div>
          <div class="size-c">1年</div>
          <span class="color-c size-a">投保期限</span>
        </div>
      </div>
    </a>
    
    
    export default {
      name: "insuresub",
      data(){
        return {
          list:this.post
        }
      },
      props:['post'],
      methods: {
        next: function(e){
          console.log(e)
          this.list = {
            idx: 'c1',
            title: '医疗险',
            page: '2',
            count: '12',
            sub:[{
              price: '198'
            },{
              price: '280'
            }]
          }
        }
      }
    }

下图看效果

1560217951968

目录
相关文章
|
5月前
Each child in a list should have a unique “key“ prop. Check the render method的报错解决
Each child in a list should have a unique “key“ prop. Check the render method的报错解决
Unknown custom element: <add-employee> - did you register the component correctly? For red cursive c
原因: 1.组件名没写对(导入和注册不对应) 2.components少写了个s 3.组件命名最好是驼峰命名 4.导入时语法错误 5.代码中有两个components,后一个的值把前一个覆盖了 6.组件直接循环套用了
|
前端开发 JavaScript
前端vue:解决Invalid prop: type check failed for prop “model“. Expected Object, got Array问题
前端vue:解决Invalid prop: type check failed for prop “model“. Expected Object, got Array问题
1348 0
前端vue:解决Invalid prop: type check failed for prop “model“. Expected Object, got Array问题
|
6月前
|
JavaScript
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
75 1
|
6月前
|
小程序 JavaScript
Avoid mutating a prop directly since the value will be overwritten whenever the parent comp
Avoid mutating a prop directly since the value will be overwritten whenever the parent comp
|
6月前
|
JavaScript
[Vue warn]: Method “components“ has type “object“ in the component definition. Did you reference the
[Vue warn]: Method “components“ has type “object“ in the component definition. Did you reference the
|
11月前
|
JavaScript
[Vue warn]: Unknown custom element: <Top> - did you register the component correctly?
[Vue warn]: Unknown custom element: <Top> - did you register the component correctly?
VS2005 error C2864 only static const integral data members can be initialized within a class
VS2005 error C2864 only static const integral data members can be initialized within a class
|
JavaScript
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent .(二)
1.在data中声明一个变量接收props的值,然后再去改变data里的这个值 2. 用computed属性 3.用data保存数据,watch监听