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

vue之prop與$emit的用法說明

 更新時間:2022年04月11日 09:48:55   作者:qq_33779502  
這篇文章主要介紹了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一個對象, 深拷貝您需要傳的對象

寫給有點時間看的人

作為一個半路出家的偽前端, 遇到坑, 基本都是因為自己基礎(chǔ)知識不牢固, 例如今天遇到這個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>

基礎(chǔ)真的很重要~

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

相關(guān)文章

  • vue3 ts組合式API異常onMounted is called when there is no active component解決

    vue3 ts組合式API異常onMounted is called when&

    這篇文章主要為大家介紹了vue3 ts組合式API異常onMounted is called when there is no active component問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 關(guān)于Vue中過濾器的必懂小知識

    關(guān)于Vue中過濾器的必懂小知識

    vue過濾器可以在不改變原始數(shù)據(jù),只是對數(shù)據(jù)進行加工處理后返回過濾后的數(shù)據(jù)再進行調(diào)用處理,下面這篇文章主要給大家介紹了關(guān)于Vue中過濾器必懂小知識的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • Vue之過濾器詳解

    Vue之過濾器詳解

    這篇文章主要為大家介紹了Vue之過濾器,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助,希望能夠給你帶來幫助
    2021-11-11
  • Vue 2中ref屬性的使用方法及注意事項

    Vue 2中ref屬性的使用方法及注意事項

    這篇文章主要給大家介紹了關(guān)于Vue 2中ref屬性的使用方法及注意事項的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。
    2017-06-06
  • vue中form表單禁用專用組件介紹

    vue中form表單禁用專用組件介紹

    這篇文章主要介紹了vue中form表單禁用專用組件,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 解決Vue @submit 提交后不刷新頁面問題

    解決Vue @submit 提交后不刷新頁面問題

    這篇文章主要介紹了解決Vue @submit 提交后不刷新頁面問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue2實現(xiàn)移動端上傳、預覽、壓縮圖片解決拍照旋轉(zhuǎn)問題

    vue2實現(xiàn)移動端上傳、預覽、壓縮圖片解決拍照旋轉(zhuǎn)問題

    這篇文章主要介紹了vue2實現(xiàn)移動端上傳、預覽、壓縮圖片解決拍照旋轉(zhuǎn)問題,需要的朋友可以參考下
    2017-04-04
  • vuex與組件聯(lián)合使用的方法

    vuex與組件聯(lián)合使用的方法

    Vuex 是一個專為 Vue.js 應用程序開發(fā)的狀態(tài)管理模式。這篇文章主要介紹了vuex與組件聯(lián)合使用的方法,需要的朋友可以參考下
    2018-05-05
  • 詳細聊聊vue中組件的props屬性

    詳細聊聊vue中組件的props屬性

    父子組件之間的通信就是props down,events up,父組件通過屬性props向下傳遞數(shù)據(jù)給子組件,子組件通過事件events 給父組件發(fā)送消息,這篇文章主要給大家介紹了關(guān)于vue中組件的props屬性的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • vue使用中的內(nèi)存泄漏【推薦】

    vue使用中的內(nèi)存泄漏【推薦】

    內(nèi)存泄露是指new了一塊內(nèi)存,但無法被釋放或者被垃圾回收。這篇文章主要介紹了vue使用中的內(nèi)存泄漏,需要的朋友可以參考下
    2018-07-07

最新評論