最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過實(shí)例講解vue組件之間的傳值

 更新時(shí)間:2022年09月15日 10:13:32   作者:KinHKin(五年前端)  
通過Vuex共享數(shù)據(jù),官方給出的跨多組件傳遞數(shù)據(jù)的解決方案,下面這篇文章主要給大家介紹了關(guān)于vue組件之間傳值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

目前在做vue的項(xiàng)目,用到了子組件依賴其父組件的數(shù)據(jù),進(jìn)行子組件的相關(guān)請求和頁面數(shù)據(jù)展示,父組件渲染需要子組件通知更新父組件的state,父子組件之間的傳值一般有三種方法:

  • 父傳子
  • 子傳父
  • 非父子傳值

注意:

父子組件的關(guān)系可以總結(jié)為 prop 向下傳遞,事件向上傳遞。父組件通過 prop 給子組件下發(fā)數(shù)據(jù),子組件通過事件給父組件發(fā)送消息。

 接下來,我們會(huì)通過實(shí)例代碼來看的更清晰,理解更容易:

1.父組件向子組件進(jìn)行傳值

 父組件代碼:

<template>
  <div>
    父組件:
    <el-input v-model="val" style="width:300px" />
    <child :value="val" />
  </div>
</template>
  
  <script>
  import child from './child.vue'
  
  export default {
    name: 'Parent',
    data() {
      return {
        val: '我是父組件'
      }
    },
    components: {
      child
    },
  
  }
  </script>

子組件代碼:

<template>
 
    <div class="child">
        子組件: {{  value  }}
    </div>
 
</template>
  
  <script>
 
export default {
    name: 'App',
    data() {
        return {
        }
    },
    props: ['value']
 
}
</script>
  <style scoped>
  .child {
      margin-top: 20px;
  }
  </style>
  

2.子組件向父組件進(jìn)行傳值

父組件代碼

<template>
  <div>
    父組件:
    <el-input v-model="val" style="width:300px" />
    <child :value="val" @bindMsg='msgFun' />
  </div>
</template>
  
  <script>
  import child from './child.vue'
  
  export default {
    name: 'Parent',
    data() {
      return {
        val: '我是父組件'
      }
    },
    components: {
      child
    },
    methods: {
      msgFun(childVal) {
        console.log(childVal,'childVal')
        this.val = childVal
      }
    }
  
  }
  </script>

子組件代碼

<template>
    <div class="child">
        子組件: {{  value  }}
        <el-button @click="$emit('bindMsg', '我是子組件')">點(diǎn)擊改變父組建數(shù)據(jù)</el-button>
    </div>
</template>
  
  <script>
export default {
    name: 'App',
    data() {
        return {
        }
    },
    props: ['value'],
}
</script>
  <style scoped>
  .child {
      margin-top: 20px;
  }
  </style>
  

3.非父子組件之間的傳值 

.sync可以幫我們實(shí)現(xiàn)父組件向子組件傳遞的數(shù)據(jù)的雙向綁定,所以子組件接收到數(shù)據(jù)后可以直接修改,并且會(huì)同時(shí)修改父組件的數(shù)據(jù)

ref綁定在子組件上,引用的指向就是子組件的實(shí)例,父組件可以通過 ref 主動(dòng)獲取子組件的屬性或者調(diào)用子組件的方法

父組件代碼

<template>
  <div>
    父組件:
    <el-input v-model="val" style="width:300px" />
    <el-button @click="childRefClick">父組件ref點(diǎn)擊</el-button>
    <child :value="val" @bindMsg='msgFun' :data.sync='data' ref='child' />
  
 
  </div>
</template>
  
  <script>
  import child from './child.vue'
  
  export default {
    name: 'Parent',
    data() {
      return {
        val: '我是父組件',
        data: ''
      }
    },
    components: {
      child
    },
    methods: {
      msgFun(childVal) {
        console.log(childVal, 'childVal')
        this.val = childVal;
  
      },
      childRefClick() {
        //ref獲取子組件實(shí)例的屬性和方法
        const child = this.$refs.child
        console.log(child.name)
  
        child.childBtnClick("調(diào)用了子組件的方法")
      }
    }
  }
  </script>

子組件代碼

<template>
 
    <div class="child">
        子組件: {{  value  }}
        <el-button @click="childBtnClick">點(diǎn)擊改變父組建數(shù)據(jù)</el-button>
    </div>
 
</template>
  
  <script>
 
export default {
    name: 'App',
    data() {
        return {
            currenData: {}
        }
    },
    props: ['value', 'data'],
 
    methods: {
        childBtnClick(val) {
            console.log(val,'val')
            this.$emit('bindMsg', val || '我是子組件')
        },
    
    },
}
</script>
  <style scoped>
  .child {
      margin-top: 20px;
  }
  </style>
  

非父子組件之間的傳值方式還有slot插槽,vuex數(shù)據(jù)狀態(tài)管理器等等

總結(jié)

主要用到了父子組件的傳值,props,$emit,ref,sync等方法,父子組件之間的傳值,十分常見,只要我們用會(huì)了組件之間的傳數(shù)據(jù)的方法,對(duì)于前端的組件抽離,性能提升都有很大的好處。

到此這篇關(guān)于vue組件之間的傳值的文章就介紹到這了,更多相關(guān)vue組件間傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

涪陵区| 宝兴县| 阜康市| 永仁县| 平罗县| 炎陵县| 牙克石市| 六枝特区| 文安县| 旅游| 综艺| 基隆市| 泰宁县| 肇东市| 石城县| 突泉县| 夏河县| 吴忠市| 昂仁县| 合肥市| 天祝| 高碑店市| 辽中县| 玉溪市| 石台县| 恩施市| 乌兰浩特市| 鄂伦春自治旗| 盈江县| 芜湖县| 腾冲县| 仁化县| 岢岚县| 河源市| SHOW| 红原县| 巴彦县| 雷波县| 包头市| 孟津县| 乐安县|