Avoid mutating a prop directly since the value will be overwritten whenever the parent comp

简介: Avoid mutating a prop directly since the value will be overwritten whenever the parent comp

最近用vue练习项目,想在子组件中使用props中的值,但是在mounted中没有办法拿,最后终于搞明白了

这里我简单说一下父组件给子组件传值的两种情况(静态跟动态的数据)。

父组件中:

<template>
    <div class="order">
      <!-- 子组件  父组件通过属性传值  属性用-链接,值用驼峰命名--> 
    <order-header :order-data="orderData" :order-data1="orderData1"></order-header>
  </div>
</template>
<script>
export default{
  data(){
    return {
      orderData:[],    //这里的数据是动态的, (可以是从后端获取的数据)
      orderData1:[1,2,3]  //这里为静态值直接传给子组件,并且不发生变化
    }
  },
  mounted(){
    this.getOrder();
  },
  methods:{
    //获取数据
    getOrder(){
      axios.post(api,{
      }).then((res)=>{
        console.log(res);
        //将请求的数据放在orderData中    //这里的res.data.pageData是数组
        this.orderData =  res.data.pageData
      }).catch((err)=>{
        console.log(err);
      })
    }
  }
}
</script>

分析

父组件传递给子组件传递两个值,orderData1传递静态不变的值,orderData传递动态的值

子组件:

export default{
    props:['orderData','orderData1']
}

分析

子组件中接收用props,在template中可以直接使用{{orderData}}

子组件的methods中想要取到props中的值,

直接使用this.orderData1,但这种情况只能获取静态的值,所以只能获取orderData1,而orderData的值是动态的,如果用this.**获取到的是空或者是默认值

解决方法使用watch 重点

//  这里单独解释orderData(动态)
props:['orderData'],
data(){
  return {
    newOrderData:[]
  }
 },
 methods:{
  order(){
      console.log(this.newOrderData)     
    }
 },
watch:{
    orderData:function(newData,oldData){
      console.log(newData)  //newData就是orderData
      this.newOrderData = newData;     
      //  methods的函数在这里调用可以获取到newOrderData的值        
      this.order();
    }
  }

注:监听的值,如果有空转变时就触发,拿到值之后的处理方法也要在watch中运行

结论

props接收静态数据用this.**

动态数据用watch

如有不足之处,还望不吝赐教

喜欢的话可以体验我的小程序

微信小程序搜:红旗头像制作

相关文章
|
4月前
|
JavaScript
鬼火起~为什么报错[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the
鬼火起~为什么报错[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the
|
4月前
|
JavaScript
Property “selectedItemIndex“ was accessed during render but is not defined on instance. 报错解决
Property “selectedItemIndex“ was accessed during render but is not defined on instance. 报错解决
119 0
|
9月前
|
JavaScript
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent .(一)
大致意思就是props接收到的从父组件传过来的tableData不能直接修改。
|
9月前
|
JavaScript
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent .(二)
1.在data中声明一个变量接收props的值,然后再去改变data里的这个值 2. 用computed属性 3.用data保存数据,watch监听
|
JavaScript 算法 前端开发
Property xxx was accessed during render but is not defined on instance
目前el-form的model主要用表单验证的,也就是配合el-form的rules和el-form-item的prop来使用的。不信的话,你可以增加一个rules和prop(为了调用验证方法,也el-form也加一个ref属性,相当于id或者class选择器的意思),但是不写model,然后验证的话,会提示缺少model,导致无法验证成功。
Property xxx was accessed during render but is not defined on instance
Duplicate methods named spliterator with the parameters () and () are inherited from the types Colle
Duplicate methods named spliterator with the parameters () and () are inherited from the types Colle
49 0
|
JavaScript
【解决方案】[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the pa
【解决方案】[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the pa
1332 0
|
JavaScript
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 re-
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-
how to know which settype an assignment block is built based on
how to know which settype an assignment block is built based on
101 0
how to know which settype an assignment block is built based on