Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-
原因:
子组件改了父组件的值
避免修改父组件传过来的值
注意: 如果value是obj,需要进行深拷贝。
可以参考: https://blog.csdn.net/u013948858/article/details/118342541
文章目录
- 1. 需求及报错
- 2. 分析原因
- 2.1 这是什么原因造成?
- 3. 解决方案: 使用 v-on 明确实现修改方式
- 4. 参考
1. 需求及报错
需求: 使用的场景是:A 组件中引用 B 组件,使用 v-model
给 B 传递参数,B 使用 props: { value }
接收内容,在 B 中根据逻辑直接修改并赋值 value
, 事件触发后在浏览器 console 里看到报错,内容如下:
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: "value"
2. 分析原因
从报错内容上来看,我们改动了子组件中引用的父组件的变量,也就是 props 中的数据,是不能这么操作的;
从提示的信息上看,使用
mutated
是否可行?在 Vue2 中组件
props
中的数据只能单向流动,即只能从父组件通过组件的DOM
属性attribute
传递props
给子组件,子组件只能被动接收父组件传递过来的数据,并且在子组件中,不能修改由父组件传来的props
数据。组件内不能修改props的值,同时修改的值也不会同步到组件外层,即调用组件方不知道组件内部当前的状态是什么
2.1 这是什么原因造成?
在
vue1.x
版本中利用props
的twoWay
和.sync
绑定修饰符就可以实现props
的双向数据绑定。在
vue2.0
中移除了组件的props
的双向数据绑定功能,如果需要双向绑定需要自己来实现。
在vue2.0
中组件的props
的数据流动改为了只能单向流动,即只能由(父组件)通过组件的v-bind:attributes
传递给(子组件),子组件只能被动接收父组件传递过来的数据,并在子组件内不能修改由父组件传递过来的props
数据。官方文档解释:
prop 是单向绑定的:当父组件的属性变化时,将传导给子组件,但是不会反过来。这是为了防止子组件无意修改了父组件的状态——这会让应用的数据流难以理解。
虽然废弃了props的双向绑定对于整个项目整体而言是有利且正确的,但是在某些时候我们确实需要从组件内部修改props的需求
在 Vue2.0
中,实现组件属性的双向绑定方式(算不上是绑定了,算是异步修改), 可使用如下方法
v-model
指令或.sync
修饰符- 将修改属性的方法通过
v-bind
传给子组件调用,子组件直接按方法使用即可 - 将修改属性的方法通过
v-on
传递给子组件调用,使用$emit()
实现回调修改 - 或者使用
this.$parent
去修改
3. 解决方案: 使用 v-on 明确实现修改方式
也是为了代码可读性
不要直接修改
从父组件传过来的 props
数据,在data函数中重新定义一个变量,将props数据数据赋值在data变量上,后面修改data变量即可。如下:
- B 组件接受 A组件的参数
name: "B",
props: {father: {type: String,default: null}
}
data(){return{father_a : this.father}
}
http://www.taodudu.cc/news/show-805245.html
相关文章:
- 小程序 -- [sitemap 索引情况提示] 根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引
- vue 父组件传子组件数值,父值变更子获取的值没有变的问题
- 大杂烩: 踩坑记录
- sudo: apt-get:找不到命令”的解决方法
- 腾讯云linux系统结合nginx部署项目
- mac下使用n管理node版本
- npm依赖文件package.json中^和~的含义
- 使用electron-vue + frp 开发一款穿透软件
- Linux环境搭建frp实现内网穿透
- ECMAScript 新提案:JSON模块
- localtunnel:本地端口的服务映射到公网
- curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443
- sentry使用webpack上传sourceMap源文件定位错误到更详细具体的代码片段
- Mac上解决Chrome浏览器跨域问题
- vue history模式Nginx配置
- vue 页面生成pdf并下载 vue页面转PDF
- 关于parseInt面试题
- 了解冒泡排序选择排序
- .gitignore失效 无法忽略node_modules问题
- (一)梳理前端知识体系,搞定大厂必考面试题
- (四)JS基础知识一(深入分析变量类型和计算)【不会变量,别说你会JS】
- (五)JS基础知识二(通过图理解原型和原型链)【三座大山之一,必考!!!】
- (六)JS基础知识三(走进作用域和闭包)【三座大山之二,不会闭包,基本不会通过】
- (七)JS基础知识四(搞定异步)【三座大山之三,必考!!!】
- (十二)运行环境(加载、性能优化、安全)【这些会了,你就可以飞了】
- (十)HTTP协议【前后端分离的时代,网络请求是前端的生命线】
- (九)JS-WEB-API(DOM、BOM、事件、Ajax、存储)【每个工程师必须熟练掌握的技能】
- (八)JS异步进阶,更深更广搞定JS异步【想要进大厂,更多异步的问题等着你】
- (十一)开发环境【不会这些,你就会被认定是菜鸟小白,没做过项目】
- (三)CSS【不多说了,前端面试 CSS 是必考知识,不过关直接回家】
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-相关推荐
- Avoid mutating a prop directly since the value will be overwritten whenever the parent component...
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re- ...
- vue使用prop通信出错:Avoid mutating a prop directly since the value will be overwritten whenever the parent
使用props进行父子组件通信时产生错误:Avoid mutating a prop directly since the value will be overwritten whenever the ...
- Avoid mutating a prop directly since the value will be overwritten whenever the parent ...
错误:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the paren ...
- uni-app:Avoid mutating a prop directly since the value will be overwritten whenever the parent
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re- ...
- [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever...
报错 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the paren ...
- Avoid mutating a prop directly since the value will be overwritten whenever
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re- ...
- VUE报错:Avoid mutating a prop directly since the value will be overwwritten whenever the parent及解决方案
VUE报错: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the p ...
- 报错信息:Avoid mutating a prop directly since the value will be overwritten
报错信息:Avoid mutating a prop directly since the value will be overwritten whenever the parent componen ...
- avoid mutating a prop directly since the value will be overwritten whenever完美解决
在vue父组件传递数据给子组件时候,通过双向绑定给属性赋值时候,报错如下:Avoid mutating a prop directly since the value will be overwrit ...
最新文章
- golang微服务框架对比_微服务里程碑,Golang与Spring Cloud Alibaba完美结合
- Linux进程模型总结
- Python基础综合练习
- AI 玩微信跳一跳的正确姿势:跳一跳 Auto-Jump 算法详解
- 程序员基本功10栈和队列
- 碎片脚本注解(后续整理)
- Word2Vec教程-Skip-Gram模型
- Django Form ModelForm modelfromset
- java泛型方法的使用
- Scrapy爬取多层级网页内容的方式
- ES新特性之Reflect对象
- 决策引擎EngineX平台实践
- 2020最新手机百度云不限速教程,下载速度10M/S,比会员还快
- 组合数据类型(序列:列表)
- 芯昇,XS5013,ISP+TX,模拟高清,AHD
- apktool反编译及后续打包
- Topsis模型小记
- 用win7系统如何登录ftp服务器,必备技巧之最详细win7系统登陆ftp服务器的图文教程...
- 高定服装走向NFT,巴黎时装周的数字化将带来什么新趋势?
- 华侨大学计算机组成原理期末考试试题,计算机组成原理试卷A卷及答案