当我们直接改变父组件的 props 值是会报以下警告:

[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: “facomment”

会报错的原因是由于 Vue 的单项数据流

怎样理解 Vue 的单项数据流?

  • 数据总是从父组件传到子组件,子组件没有权利修改父组件传过来的数据,只能请求父组件对原始数据进行修改。这样会防止从子组件意外改变父组件的状态,从而导致你的应用的数据流向难以理解。
  • 注意: 在子组件直接用 v-model 绑定父组件传过来的 props 这样是不规范的写法,开发环境会报警告。
  • 如果实在要改变父组件的 props 值可以再data里面定义一个变量,并用 prop 的值初始化它,之后用$emit 通知父组件去修改。

解决方案:

父组件:

<template><div><BlogComment :facomment="comment" @recordsChange="recordsChange"/></div>
</template><script>
export default {name: "BlogDetail",components: {BlogComment},data(){return {comment: {},}},methods: {recordsChange(records) {this.comment = records; //在父组件修改值},}
};
</script>

子组件:

<template><div><div class="pagination-box" v-if="facomment.records != null"><el-paginationbackgroundlayout="prev, pager, next":current-page="facomment.current":page-size="facomment.size":total="facomment.total"@current-change="page1"></el-pagination></div></div>
</template><script>
export default {name: "",props: ["facomment"],methods: {// 分页page1(page) {this.$axios.get("/comment/list", {params: {blogId: this.blogId,page: page,pageSize: this.pageSize,},}).then((res) => {// this.facomment = res.data.data (不能直接修改)this.$emit("recordsChange", res.data.data); // 用 $emit 通知父组件去修改});},}
};
</script>

【Vue】报错:Avoid mutating a prop directly since the value will be overwritten whenever the parent相关推荐

  1. VUE报错__Avoid mutating a prop directly since the value will be overwritten whenever the parent

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

  2. vue 报错avoid mutating a prop directly since the value will be overwritten whenever

    这里写自定义目录标题 avoid mutating a prop directly since the value will be overwritten whenever 子组件修改父组件值时报的错 ...

  3. 前端开发:Vue报错Avoid mutating a prop directly since the value will be…的解决方法

    前言 前端开发中,在使用Vue开发的时候,经常会遇到一些很共性的报错提示,而且有时候提示已经很明确的告诉开发者问题出现在哪里,尤其是在Chrome的控制台输出上面,只要看一下输出日志就知道问题所在.但 ...

  4. vue修改props传过来的值报错Avoid mutating a prop directly since the value will be overwritten whenever the par

    在做项目时有时候会遇到这种错误 这句话的意思是说,避免直接改变道具,因为只要父组件重新渲染,该值就会被覆盖.相反,使用基于道具值的数据或计算属性. 就是不能直接改变父组件传过来的props,所以怎么办 ...

  5. 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 ...

  6. Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-

    原因: 子组件改了父组件的值 避免修改父组件传过来的值 注意: 如果value是obj,需要进行深拷贝. 可以参考: https://blog.csdn.net/u013948858/article/ ...

  7. 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- ...

  8. 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 ...

  9. 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- ...

  10. vue父子组件赋值操作时报错Avoid mutating a prop directly since the value will be overwritten whenever

    前言: 我写了一个组件,然后子组件外层绑定了一个v-model='moreInquiriesCard', 这个值通过props取的父组件传过来的值,子组件是个modal弹框,对默认右上角有个" ...

最新文章

  1. manjaro无效的软件包
  2. MySQL 5.7最新版本的2个bug
  3. java的语法树,JAVA语言语法树.doc
  4. unix中的grep家族
  5. sudo uograde 之后 需要重装显卡驱动
  6. excel字符处理函数
  7. 【LaTex】Vscode+LaTex模板的使用
  8. mmdetection学习之anchor_generator
  9. 互联网协议以及网络分层
  10. 可道云 docker 群晖_利用群晖NAS同步文献
  11. 大学英语四新视野 课后习题+答案翻译 Unit1~Unit8
  12. Flask Request对象
  13. oc贴材质透明logo有问题,logo贴图有底色,oc贴图怎么不平铺
  14. 【工程光学】典型光学系统
  15. Android 获取蓝牙设备类型
  16. 【图像去噪】基于matlab高斯+均值+中值+双边滤波图像去噪【含Matlab源码 1872期】
  17. [笔记]Windows核心编程《二十》DLL的高级操作技术
  18. python控制excel选择区域_python针对excel的操作技巧
  19. 串口通信——S5PV210串口通信接口详解
  20. Linux服务器连接校园网

热门文章

  1. Python中向列表添加元素的方法
  2. 多视图多行为对比学习推荐系统
  3. 读书札记:30个因素预示美国将再现“大萧条”...
  4. 1754. 骑士精神
  5. lighttpd支持AJAX吗,lighttpd配置https
  6. nginx 区分手机浏览器和pc浏览器
  7. macOS High Sierra 10.13.4 安装n2n
  8. 漫谈区块链“羊群效应”
  9. 计算机中文输入法教案,智能ABC输入法教案
  10. Oracle随机抽样sample使用说明