[Vue warn]: 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 parent component re-renders. Instead, use a data or computed property based on the prop’s value. Prop being mutated: “app”

[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: "app"

这样的错误是我在昨天遇到的,今天反过来再看一遍官方文档,看到了解释。官方文档中文截图如下:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent_第1张图片
英文截图如下:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent_第2张图片

又有点新东西

今天再次遇到了这个问题,根据官方文档的翻译,我们可以理解为props中的属性最好避免改动,已经提醒你了,出错了算你自己的
如果我们一定要改动这些值,我们可以在data中重新定义相应的属性来接收props中的属性。例:

<template>
  <div>
    <span>子组件</span>
    <input type="text" name="" v-model.trim="textTip">
    <button type="button" class="btn btn-success btn-xs" v-on:click="callFather">通知父组件</button>
  </div>
</template>
<script>


export default {
  data() {
    return {
      textTip:this.propTextTip
    }
  },
   props:['propTextTip'],
  methods:{
      callFather:function() {
          //发射信号
          console.log(this.textTip);
          this.$emit('getCalled',this.textTip);
          //其中 getCalled 是一个自定义的事件,功能类似于一个中转
      }
  }
}

</script>

我们很明显的可以看到上述代码中,就用textTip接收了props中的propTextTip,这样可以成功的避免上述问题。
PS:感慨一下,不间断地返回来看看官方文档,还是很有收获的,平时的一些小错误,都是躺在官方文档里面。书读百遍其义自见,古人智慧,万古真理。

代码来源:https://www.cnblogs.com/cristina-guan/p/9269275.html

你可能感兴趣的:(vue前端)