zoukankan      html  css  js  c++  java
  • vue---props进行双向数据绑定报错

    在使用vue进行组件开发的时候,遇到一个问题,父组件传递到子组件里面的值,如果在子组件里面进行改变 传递过来的"值",会报错:

    [Vue warn]: Avoid 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: "result" (found in component )

    原因:props 传递数据流是单向的,父组件传递到子组件是单向的。

    例如:父组件传递值

    <tabs :tabs="tabs" :activeIndex="activeIndex" ></tabs>

    子组件接受值:

    <template>
    <el-tabs type="card" v-model="activeIndex">
      <el-tab-pane v-for="(item,index) in tabs" :label="item"  :closable="index==0?false:true" :name="index.toString()">    
      </el-tab-pane>
    </el-tabs>
    </template>
    
    <script>
    export default{
      data(){
        return {
          tabs:[]
        }
      },
      props:['activeIndex']
    }
    </script>

    我们在父组件改变 activeIndex 的值,子组件会发生变化,但是我们注意到在子组件同样也会改变值,就会产生:

    Avoid mutating a prop directly since the value will be overwritten ....的报错

    原因在于:父子组件的传递机制

    解决办法:在子组件使用该值的时候,可以通过定义变量(currentIndex)来进行传递,这个值发生改变的时候,不会造成activeIndex的改变:

    <script>
    export default{
      data(){
        return {
          tabs:[],
          currentIndex:this.activeIndex
        }
      },
      props:['activeIndex']
    }
    </script>

     基本上这样处理,就能够解决报错问题了。

    但是我们想要子组件的 currentIndex 发生改变的时候,父组件的 activeIndex 也要发生改变,要怎么做呢?

    我们知道:activeIndex 是通过父组件传递过来的,用 props 进行接受,但是数据流动是单向,也就是在子组件的 currentIndex 发生改变,父组件的 activeIndex不会变。

    这要怎么解决?

    使用 $emit 的目的在于:实现子组件向父组件的通信;

    具体实现:定义 $emit的方法[ indexChange ] 并进行传递:

    <tabs :tabs="tabs" :activeIndex="activeIndex" @indexChange="indexChange"></tabs>
    <el-button @click="change">改变</el-button>
    <script>
    export default{
      data(){
        return {
          tabs:[],
          activeIndex:1;
        }
      },
      methods:{
        change(){
          this.activeIndex = 2;
        },
        indexChange(index){
          this.activeIndex = index;
        },
      }
    }
    </script>

    子组件监听:currentIndex 的变化并向父组件进行通信:

    <template>
    <el-tabs type="card" v-model="activeIndex">
      <el-tab-pane v-for="(item,index) in tabs" :label="item"  :closable="index==0?false:true" :name="index.toString()">    
      </el-tab-pane>
    </el-tabs>
    </template>
    
    <script>
    export default{
      data(){
        return {
          tabs:[],
          currentIndex:this.activeIndex
        }
      },
      props:['activeIndex'],
      watch:{
        currentIndex:function(index){      
          this.$emit('indexChange',index);
        }
      }
    }
    </script>
  • 相关阅读:
    Ubuntu根底入门教程
    Debian发行版总结
    ARCH中KDEMOD下如何管理挂在NTFS分区乱码的标题问题
    让YUM窗口化
    操持SUSE Linux 10下无法显示阻碍一例
    让debian支撑鼠标中键
    电视片头后期合成软件、制作流程与技巧简介
    关于动态数组、静态数组转换为字符串的一些问题 给 "厨师" 的回复
    论证 Assigned(btn) 与 Assigned(@btn) 的区别 答复 "sunnet"
    WinAPI: GetRegionData
  • 原文地址:https://www.cnblogs.com/e0yu/p/11264659.html
Copyright © 2011-2022 走看看