vue之prop與$emit的用法說明
prop與$emit的用法
1.vue組件Prop傳遞數(shù)據(jù)
組件實例的作用域是孤立的,這意味著不能在子組件的模板內(nèi)直接引父組件的數(shù)據(jù),如果要讓子組件使用父組件的數(shù)據(jù),則需要通過子組件的prop選項;prop是單向綁定的,當父組件的屬性變化時,將傳遞給子組件,但是反過來不行;這樣主要是防止子組件無意修改父組件的狀態(tài);每次父組件更新時,子組件的所有prop都會更新為最新值。這意味著你不能在子組件內(nèi)部改變prop。
2.子組件可以使用$emit觸發(fā)父組件的自定義事件
vm.$emit( event, arg ):發(fā)送數(shù)據(jù),第一個參數(shù)是發(fā)送數(shù)據(jù)的名稱,接收時還用這個參數(shù)接收,第二個參數(shù)是這個數(shù)據(jù)現(xiàn)在的位置
拓展:
vm.$on( event, fn ):接收數(shù)據(jù),第一個參數(shù)是數(shù)據(jù)的名稱,與發(fā)送時的名字對應,第二個參數(shù)是一個方法,要對數(shù)據(jù)的操作
注:vue模板只能有一個根對象 (在template中只能用一個標簽來包裹全部元素)不然會報錯如:
Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
父組件:
<template>
? <div>
? ? ?<div>父組件的addName:{{addrName}}</div>
? ? <child-prop @showAddrName="updateAddrName" :sendData="addrName"></child-prop>
? </div>
</template><script>
? import childProp from "./childProp";
? export default {
? ? name:'index',
? ? components: {childProp},
? ? data () {
? ? ? return {
? ? ? ? addrName:"北京"
? ? ? }
? ? },
? ? methods:{
? ? ? updateAddrName(data){//觸發(fā)子組件城市選擇-選擇城市的事件
? ? ? ? console.log(data);
? ? ? ? this.addrName = data.addrName;//改變了父組件的值
? ? ? ? console.log('toCity:'+this.addrName)
? ? ? }
? ? }
? }
</script>子組件:
<template>
? <div>
? ? <h3>父組件傳給子組件的addrName:{{sendData}}</h3>
? ? <br/><button @click='addr(`上海`)'>點擊此處將‘上海'發(fā)射給父組件</button>
? </div>
</template><script>
? export default {
? ? name:'childProp',
? ? props:["sendData"], // 用來接收父組件傳給子組件的數(shù)據(jù)
? ? methods:{
? ? ? addr(val) {
? ? ? ? let data = {
? ? ? ? ? addrName: val
? ? ? ? };
? ? ? ? this.$emit('showAddrName',data);//select事件觸發(fā)后,自動觸發(fā)showCityName事件
? ? ? }
? ? }
? }
</script>今天遇到的坑--this.$emit
寫給趕時間的人
一句話
this.$emit('xxx', input), input最好是字符串, 如果需要傳一個對象, 那么, 建議您在父組件里面, JSON.parse(input), 或者不要傳原始對象, 需要const一個對象, 深拷貝您需要傳的對象
寫給有點時間看的人
作為一個半路出家的偽前端, 遇到坑, 基本都是因為自己基礎知識不牢固, 例如今天遇到這個this.$emit的坑
需求
一個簡單的需求, 頁面上面有一個搜索框, 里面需要填2個字段, 按確定進行搜索
實現(xiàn)
我是這樣想的, 填兩個字段, 那我就把他們寫在一個對象里面, this.$emit的時候, 傳這個對象的值就好了
我的實現(xiàn)方法
search組件
<template> ? <div> ? ? <div>search</div> ? ? <input type="text" v-model="searchKey.key_apple"> ? ? <input type="text" v-model="searchKey.key_blackBerry"> ? ? <button @click="onSubmit">確定</button> ? </div> </template>
<script>
export default {
? data() {
? ? return {
? ? ? searchKey: {
? ? ? ? key_blackBerry: "",
? ? ? ? key_apple: ""
? ? ? }
? ? };
? },
?
? methods: {
? ? onSubmit() {
? ? ? this.$emit("onSearchSubmit", this.searchKey);
? ? }
? }
};
</script>父組件:
<template>
? <section class="container">
? ? <div>
? ? ? <Search @onSearchSubmit="onSearchSubmit"/>
? ? ? <h1>{{parent_search}}</h1>
? ? </div>
? </section>
</template><script>
import Search from "~/components/Search.vue";
?
export default {
? components: {
? ? Search
? },
?
? data() {
? ? return {
? ? ? parent_search: {}
? ? };
? },
?
? methods: {
? ? onSearchSubmit(input) {
? ? ? this.parent_search = input;
? ? }
? }
};
</script>效果
實際上也能達到要求, 但是, 出現(xiàn)了一個意想不到的結(jié)果: 當?shù)谝淮吸c擊確定之后, 我們再在搜索框里面輸入, 預想的結(jié)果是什么都沒變化, 例如h1里面的字符不會變化, 但是, 結(jié)果確發(fā)現(xiàn), 雙向綁定了, 這不是我想要的結(jié)果...我并沒有實現(xiàn)父子組件間的雙向綁定(例如通過復寫組件的change方法)
問題來了,問題解決
發(fā)生這個情況的原因在于, 我寫的自組件this.$emit里面, 是一個對象, 其實傳的是它的地址
所以,后面這樣改寫子組件就ok了
<template> ? <div> ? ? <div>search</div> ? ? <input type="text" v-model="searchKey.key_apple"> ? ? <input type="text" v-model="searchKey.key_blackBerry"> ? ? <button @click="onSubmit">確定</button> ? </div> </template>
<script>
export default {
? data() {
? ? return {
? ? ? searchKey: {
? ? ? ? key_blackBerry: "",
? ? ? ? key_apple: ""
? ? ? }
? ? };
? },
?
? methods: {
? ? onSubmit() {
? ? ? const input = JSON.parse(JSON.stringify(this.searchKey));
? ? ? this.$emit("onSearchSubmit", input);
? ? }
? }
};
</script>基礎真的很重要~
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
從0到1構建vueSSR項目之node以及vue-cli3的配置
這篇文章主要介紹了從0到1構建vueSSR項目之node以及vue-cli3的配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
使用webpack打包后的vue項目如何正確運行(express)
這篇文章主要介紹了使用webpack打包后的vue項目如何正確運行(express) ,接下來通過本文給大家詳細介紹,需要的朋友可以參考下2018-10-10
antd?Vue實現(xiàn)Login登錄頁面布局案例詳解?附帶驗證碼驗證功能
這篇文章主要介紹了antd?Vue實現(xiàn)Login登錄頁面布局案例詳解附帶驗證碼驗證功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
vue結(jié)合vant實現(xiàn)聯(lián)動效果
這篇文章主要為大家詳細介紹了vue結(jié)合vant實現(xiàn)聯(lián)動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
VUE如何實現(xiàn)點擊文字添加顏色(動態(tài)修改class)
這篇文章主要介紹了VUE如何實現(xiàn)點擊文字添加顏色(動態(tài)修改class),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

