欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue組件傳值方式(props屬性,父到子,子到父,兄弟傳值)

 更新時(shí)間:2024年06月07日 09:45:16   作者:web Rookie  
這篇文章主要介紹了Vue組件傳值方式(props屬性,父到子,子到父,兄弟傳值),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

Vue是數(shù)據(jù)驅(qū)動(dòng)視圖更新的框架,平時(shí)寫業(yè)務(wù)時(shí),都會(huì)把頁(yè)面不同模塊拆分成一個(gè)一個(gè)vue組件, 所以對(duì)于vue來(lái)說(shuō)組件間的數(shù)據(jù)通信非常重要

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、props是什么?

  • propsVue實(shí)例上的一個(gè)屬性,用于組件的傳值
  • 作用:為了接收外面?zhèn)鬟^(guò)來(lái)的數(shù)據(jù),與data、methods等是一個(gè)級(jí)別的配置項(xiàng)。
  • props在子屬性和父屬性之間形成一個(gè)單向向下的綁定(單向數(shù)據(jù)流)
  • 當(dāng)父屬性更新時(shí),它會(huì)向下流向子屬性,但不會(huì)反過(guò)來(lái)。這可以防止子組件意外改變父組件的狀態(tài)

使用規(guī)則

<script>
export default {
	//使用方式一
    props:["count"],
    //使用方式二
    props:{
        count:{
        	//type:類型規(guī)定
            type:Number,
            //default:默認(rèn)值
            default:0,
            //require:是否是必傳
            require:true,
        }
    },
}
</script>

二、父?jìng)髯?props

實(shí)現(xiàn)步驟

  • 父?jìng)髯油ㄟ^(guò) props實(shí)現(xiàn)
  • 父組件通過(guò)自定義變量傳入子組件
  • 子組件利用props接收父組件值
  • 接收props不能修改,但是可以在組件中直接使用
  • 如果想要修改props,先把props賦值給其他變量,在進(jìn)行修改

代碼實(shí)現(xiàn)

//父組件
<template>
  <div style="outline:1px solid red">
    <h1>我是父組件</h1>
    <button @click="count+=1">要傳遞的值{{count}}</button>
    //第三步通過(guò)v-bind動(dòng)態(tài)傳入子組件
    <Child :count="count"></Child>
  </div>
</template>

<script>
//第一步導(dǎo)入組件
import Child from "../child/index.vue"
export default {
//第二步組件注冊(cè)
    components:{
        Child,
    },
    data () {
    return {
      count: 1
    }
  }
}
//子組件
<template>
  <div style="outline:1px solid green">
    <h1>我是子組件</h1>
    // 第二步在頁(yè)面中使用
    <div>接收父組件傳值:{{count}}</div>
  </div>
</template>

<script>
export default {
	//第一步利用 props 接收
	//可以直接在頁(yè)面中使用,但不可修改;修改需要先將 props 中的數(shù)據(jù)賦值給 data 中的變量
	// props:["count"],
    props:{
        count:{
            type:Number,
            default:0,
            require:true,
        }
    },
}

效果展示

二.子傳父 $emit

實(shí)現(xiàn)步驟

  • 子傳父通過(guò) $emit 實(shí)現(xiàn)
  • 在子組件中通過(guò) $emit 方法傳值給父組件
  • 在父頁(yè)面中的子組件標(biāo)簽中自定義事件接收

代碼實(shí)現(xiàn)

//子組件
<template>
  <div style="outline:1px solid green">
    <h1>我是子組件</h1>
    <button @click="give">傳遞給父組件</button>
  </div>
</template>

<script>
export default {
  methods:{
    give(){
	 //第一步調(diào)用 $emit 傳遞參數(shù)
	 //emit 中第一個(gè)參數(shù)是在父頁(yè)面中接收的 事件名稱
	 //emit 中第二個(gè)嘗試是要傳遞的數(shù)據(jù)
      this.$emit("child",`我是子組件值+ ${new Date().getTime()}`)
    }
  }
}
// 父組件
<template>
  <div style="outline:1px solid red">
    <h1>我是父組件</h1>
       <div>我是接收子組件的值:{{this.childValue}}</div>
       // 第一步在子組件中自定義事件接收 emit 傳遞的事件
    <Child :count="count" @child="accept"></Child>
  </div>
</template>

<script>
import Child from "../child/index.vue"
export default {
    components:{
        Child,
    },
    data () {
    return {
      childValue:"",
    }
  },
  methods:{
  	// 第二步在事件接收使用 子組件的傳值
    accept(obj){
      this.childValue = obj
    }
  }
}

效果展示

三.兄弟傳值 EventBus

實(shí)現(xiàn)步驟

  • 兄弟之間傳值通過(guò) EventBus實(shí)現(xiàn)
  • 在components文件中新建一個(gè) js 文件,頁(yè)面中導(dǎo)入Vue實(shí)例,然后導(dǎo)出一個(gè)new Vue()
  • 在需要兄弟之間傳值的組件中導(dǎo)入這個(gè)文件
  • 傳值時(shí)通過(guò)導(dǎo)入的文件調(diào)用$emit 實(shí)現(xiàn)($emit("事件名稱",需要傳遞的值))
  • 接收時(shí)通過(guò)導(dǎo)入的文件調(diào)用$on通過(guò)回調(diào)函數(shù)實(shí)現(xiàn)

代碼實(shí)現(xiàn)

// component 文件夾中新建 eventBus.js 文件,用來(lái)實(shí)現(xiàn)兄弟組件通信
import Vue from "vue";

export default new Vue()
// 組件 A
<template>
 <div style="width: 100%; height: 100px; outline: 1px solid red">
   <button @click="send">A組件給B組件傳值</button>
 </div>
</template>

<script>
import bus from "../eventBus";
export default {
 methods: {
   send() {
     bus.$emit("share", `我是A組件+ ${new Date().getTime()}`);
   },
 },
};
</script>

<style lang="less" scoped>
</style>
// 組件 B
<template>
    <div style="width:100%;height:100px;outline:1px solid green">
        接收A組件的值{{this.accept}}
    </div>
</template>

<script>
import bus from "../eventBus"
    export default {
        data(){
            return{
                accept:''
            }
        },
        mounted(){
            bus.$on("share",val=>{
                this.accept = val
            })
        }
 }
</script>

效果展示

總結(jié)

  • 父?jìng)髦底油ㄟ^(guò)props實(shí)現(xiàn)
  • 子傳父通過(guò)emit自定義事件 實(shí)現(xiàn)
  • 兄弟傳值通過(guò) eventBus實(shí)現(xiàn)

目前暫時(shí)總結(jié)三種常用的組件傳值方法

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論