vue 父子组件间的传值(一)——父传子

一、静态传值

父组件向子组件传值,子组件只要使用props接收即可。

<style scope>
    .hide{
        display:none;
    }
</style>
/*父组件*/
<template>
<div class="parent">
    <span v-for="(item,index) in message" @click="clickHandle(index)">{{item.value}}</span>
    <div class="isVisible?'hide':''">
    <!-- 父组件在此通过属性info 传值给子组件 -->
        <child info="parentIndex"></child>
    </div>
</div>
</template>
<script>
import child from './child'
  export default {
    components:{
        child
    },
    data(){
        return {
            isVisible:false,
            message:[
                {'value':'第一行'}, {'value':'第二行'}, {'value':'第三行'}
            ],
            parentIndex:null
        }
    },
    methods:{
        clickHandle(index){
            this.isVisible = true
            this.parentIndex = index
        }
    }
}
</script>
 
/*子组件*/
<template>
<div>
<!-- 子组件在此使用父组件传进来的值info -->
<div>{{info}}</div>
<div v-for="(item,index) in childData" v-if="childData">
    <span>{{item.value}}</span>
</div>
</div>
</template>
<script>
/*model文件是配置地址url的基础选项*/
import * as model from '../model'
export default{
 //子组件在此接收父组件的传值info
    props:['info'],
    created(){
        this.getData()
    },
    data(){
        return {
            childData:null
        }
    },
    watch:{
        info:{
            handle(new,old){
                this.getData()
            }
        }
    },
    methods:{
        getData(){
            /*这里写请求数据的方法*/
            //如果从父组件的数据已经得到,但此时无法判断数据和方法是否已经挂载到实例上,如果没写created方法的话
            if(this.info){
                let url = this.info + '.json'
                let childInfo = model.CacheModel.get(url)
                if(childInfo){
                    childInfo.then(res => {
                        this.childData = res.data
                        console.log(res.data)
                    })
                }
            }        
        }
    }
}
</script>

二、动态传值

项目中遇到一个问题: 需求是当子组件接收到父组件传来的值后,动态请求后台数据获取到后再显示到视图中,且子组件是在点击父组件某一项时才改变其display值为显示的。
vue有数据双向绑定机制,所以只考虑怎样动态更新数据即可,也就是什么时候触发子组件定义的根据父组件传递的值来向后台请求数据的方法。
方法:使用watch监听父组件传递过来的值,然后执行请求数据方法

普通watch
data() {
    return {
        frontPoints: 0    
    }
},
watch: {
    frontPoints(newValue, oldValue) {
        console.log(newValue)
    }
}

数组的watch

data() {
    return {
        winChips: new Array(11).fill(0)   
    }
},
watch: {
  winChips: {
    handler(newValue, oldValue) {
      for (let i = 0; i < newValue.length; i++) {
        if (oldValue[i] != newValue[i]) {
          console.log(newValue)
        }
      }
    },
    deep: true
  }
}

对象的watch

data() {
  return {
    bet: {
      pokerState: 53,
      pokerHistory: 'local'
    }   
    }
},
watch: {
  bet: {
    handler(newValue, oldValue) {
      console.log(newValue)
    },
    deep: true
  }
}

对象的具体属性watch(活用computed)

data() {
  return {
    bet: {
      pokerState: 53,
      pokerHistory: 'local'
    }   
    }
},
computed: {
  pokerHistory() {
    return this.bet.pokerHistory
  }
},
watch: {
  pokerHistory(newValue, oldValue) {
    console.log(newValue)
  }
}

bug: 第一次请求的时候,数据返回特别慢,也可能无法返回数据,而且很有可能会报错超时,原因就是当父组件传递过来的值发生改变时,想要观测数据触发请求方法时,此时无法确定实例是否已经实例化完成也就是是否完成以下的配置:数据观测(data observer),属性和方法的运算,所以无法判断数据和方法是否已经运算完毕
解决方法: created()方法中先执行一次请求数据的方法,确保数据和方法均已运算完毕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue项目中,父子组件传值有多种方法可以实现。其中一种常用的方法是使用props和$emit。 首先,在组件中,可以通过props属性将数据递给子组件。在子组件中,通过props选项接收组件递的数据。例如,在组件中定义一个子组件,并通过props属性递一个名为"name"的数据给子组件: ```html <Child :name="小张"></Child> ``` 在子组件中,通过props选项接收组件递的数据: ```javascript props: \["name"\] ``` 另一种方法是使用$emit来自定义事件,在子组件中触发该事件并递数据给组件。在组件中,通过监听子组件触发的事件来获取子组件递的数据。例如,在子组件中触发一个名为"increment"的自定义事件,并递数据"我是子组件"给组件: ```javascript this.$emit("increment", "我是子组件") ``` 在组件中,通过监听子组件触发的事件来获取子组件递的数据: ```html <Child @increment="f1"></Child> ``` ```javascript methods: { f1(data) { console.log(data) // 打印"我是子组件" } } ``` 这样,组件就可以接收到子组件递的数据了。 除了props和$emit,还有其他方法可以实现父子组件传值,如使用$parent和$children来访问组件和子组件的实例,或者使用$ref来引用子组件。但是在实际开发中,props和$emit是最常用的方法。 #### 引用[.reference_title] - *1* *3* [vue父子组件传值的方法](https://blog.csdn.net/YoungMan_09/article/details/123451827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue父子组件传值](https://blog.csdn.net/qq_49867247/article/details/123480614)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值