报错:Avoid mutating a prop directly since the value will be overwritten whenever the parent component........

原因:所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。(父组件更新,子组件中的prop值也会更新,但子组件不能修改由父组件传递过来的值)

不能直接对父组件传来的值进行双向绑定,要先子组件里定义新的变量来接收父组件传来的值,接着我们可以使用v-modal+watch属性 或者 使用:binfd="" + @input=" func"(再定义这个func通过传入的event 得到改变的值,将event.target.value赋值给新变量)

   <div id="app">
<h3>我是父组件</h3>
<templ :num-from-father="fatherData"
:num-from-father2="fatherData2"
@change1="changeFunc1"
@change2="changeFunc2"/>
</div> <template id="temp">
<div>
<h3>我是子组件</h3>
<p>props1:{{numFromFather}}</p>
<p>转存的值:{{receiveNum1}}</p>
<!-- 方法1 -->
<input type="number" :bind="receiveNum1" @input="receiveNum1Input">
<p>props2:{{numFromFather2}}</p>
<p>转存的值:{{receiveNum2}}</p>
<!-- 方法2 使用watch -->
<input type="number" v-model="receiveNum2">
</div>
</template>
<script src="/js/vue.js"></script>
<script>
const vm = new Vue({
el:'#app',
data:{
fatherData:0,
fatherData2:10
},
methods: {
changeFunc1(value){
this.fatherData = value*1;
},
changeFunc2(value){
this.fatherData2 = value*1;
}
},
components:{
templ:{
template:'#temp',
props:{
numFromFather:Number,
numFromFather2:Number,
},
data(){
return{
receiveNum1:this.numFromFather,
receiveNum2:this.numFromFather2,
}
},
methods: {
receiveNum1Input(event){
this.receiveNum1 = event.target.value;
this.$emit('change1',this.receiveNum1);
this.receiveNum2 = this.receiveNum1*100;
this.$emit('change2',this.receiveNum2);
}
},
watch: {
receiveNum2(newValue){
this.receiveNum2 = newValue;
this.$emit('change2',this.receiveNum2);
this.receiveNum1 = this.receiveNum2/100;
this.$emit('change1',this.receiveNum1);
}
},
}
}
})
</script>      

(复习)父子组件传值使用v-modal双向绑定,报错Avoid mutating a prop directly解决方案的更多相关文章

  1. Vue2.0 Props双向绑定报错简易处理办法

    在写项目的时候遇到了一个报错问题,虽然功能是正常运行,chrome的提示是:[Vue warn]: Avoid mutating a prop directly since the value wil ...

  2. vue---props进行双向数据绑定报错

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

  3. 十八、React react-router4.x中:实现路由模块化、以及嵌套路由父子组件传值

    一.路由模块化(用字典定义路由,然后循环出来) 1.官方文档参考 [官方文档]https://reacttraining.com/react-router/web/guides/quick-start ...

  4. Angular 父子组件传值

    Angular 父子组件传值 @Input  @Output  @ViewChild 新建一个头部组件 newsheader 在主组件引用 news 组件,在news组件添加 newsheader 组 ...

  5. vue 非父子组件传值

    /*非父子组件传值 1.新建一个js文件 然后引入vue 实例化vue 最后暴露这个实例 2.在要广播的地方引入刚才定义的实例 3.通过 VueEmit.$emit('名称','数据') 4.在接收收 ...

  6. 使用react进行父子组件传值

    在单页面里面,父子组件传值是比较常见的,之前一直用vue开发,今天研究了一下react的父子组件传值,和vue差不多的思路,父组件向子组件传值,父通过初始state,子组件通过this.props进行 ...

  7. 【vue】父组件主动调用子组件 /// 非父子组件传值

    一  父组件主动调用子组件: 注意:在父组件使用子组件的标签上注入ref属性,例如: <div id="home"> <v-header ref="he ...

  8. vue父子组件传值加例子

    例子:http://element-cn.eleme.io/#/zh-CN/component/form         上进行改的 父传子:用prop:子组件能够改变父组件的值,是共享的,和父操作是 ...

  9. Vue中非父子组件传值的问题

    父子组件传值的问题,前面已经讲过,不再叙述,这里来说一种非父子组件的传值. vue官网指出,可以使用一个空vue实例作为事件中央线! 也就是说 非父子组件之间的通信,必须要有公共的实例(可以是空的), ...

随机推荐

  1. Patter discovery VS clustering

    “pattern driven” (PD) is based on enumerating candidate patterns in a given solution space and picki ...

  2. ZedGraph5.1.5源码分析去掉鼠标悬浮内容闪烁问题(附源码下载)

    场景 在使用ZedGraph绘制曲线图时,将鼠标悬浮时内容闪烁,且频率很高. 找到其源码,发现不论鼠标移动的范围大小,甚至乎不论鼠标是否移动,都要刷新一次Tooltip. 注: 博客主页:https: ...

  3. 如何通过 SSH/Telnet 用 root 权限登录群晖

    出于系统安全原因,对 Synology NAS 的 root 访问有限.如果您获取 root 权限,请在命令行界面中用任何属于Local Administrators群组的帐户证书登录 DSM(如Pu ...

  4. github下载总是失败解决

    解决访问github等网站慢或下载失败的问题 第二种方法可以使用

  5. PP: Meta-learning framework with applications to zero-shot time-series forecasting

    From: Yoshua Bengio Problem: time series forecasting. Supplementary knowledge: 1. what is meta-learn ...

  6. Office办公软件Excel使用整理

    Office办公软件Excel使用整理 Excel默认打印预览于当前连接的打印机的纸张大小保持一致. Excel sheet不见了怎么办 -------------- 设置Excel第二页打印开始的位 ...

  7. i春秋公益赛 ezpload

    题目思路:一看解出的人比较多,emmm,传个马,命令执行一下.最后读到flag文件. /readflag,可执行. 题对萌新比较友好...... 考点:Linux命令,文件上传,命令执行. http: ...

  8. php文件操作(最后进行文件常用函数封装)

    文件信息相关API $filename="./1-file.php"; //filetype($filename):获取文件的类型,返回的是文件的类型 echo '文件类型为:', ...

  9. js -- 移动端pc端自动切换

    1. 判断浏览器类型 浏览器判断使用的github开源项目current-device,下面是地址: https://github.com/matthewhudson/current-device 在 ...

  10. Doing Homework HDU - 1074 状态压缩

    #include<iostream> #include<cstring> #include<cstdio> #include<string> #incl ...