vue.js 組件之間傳遞數據

原文地址:http://blog.gdfengshuo.com/2017/07/10/19html

前言

組件是 vue.js 最強大的功能之一,而組件實例的做用域是相互獨立的,這就意味着不一樣組件之間的數據沒法相互引用。如何傳遞數據也成了組件的重要知識點之一。vue

組件

組件與組件之間,還存在着不一樣的關係。父子關係與兄弟關係(不是父子的都暫稱爲兄弟吧)。git

原文做者:林鑫,做者博客:https://github.com/lin-xin/bloggithub

父子組件

父子關係便是組件 A 在它的模板中使用了組件 B,那麼組件 A 就是父組件,組件 B 就是子組件。vuex

// 註冊一個子組件
Vue.component('child', {
    data: function(){
        return {
            text: '我是father的子組件!'
        }
    },
    template: '<span>{{ text }}</span>'
})
// 註冊一個父組件
Vue.component('father', {
    template: '<div><child></child></div>'  // 在模板中使用了child組件
})

直接使用 father 組件的時候:app

<div id="app">
    <father></father>
</div>

頁面中就會渲染出 :我是father的子組件!ide

father 組件在模板中使用了 child 組件,因此它就是父組件,child 組件被使用,因此 child 組件就是子組件。模塊化

兄弟組件

兩個組件互不引用,則爲兄弟組件。this

Vue.component('brother1', {
    template: '<div>我是大哥</div>'
})
Vue.component('brother2', {
    template: '<div>我是小弟</div>'
})

使用組件的時候:spa

<div id="app">
    <brother1></brother1>
    <brother2></brother2>
</div>

頁面中就會渲染出 :

我是大哥

我是小弟

Prop

子組件想要使用父組件的數據,咱們須要經過子組件的 props 選項來得到父組件傳過來的數據。如下我使用在 .vue 文件中的格式來寫例子。

如何傳遞數據

在父組件 father.vue 中引用子組件 child.vue,把 name 的值傳給 child 組件。

<template>
    <div class="app">
        // message 定義在子組件的 props 中
        <child :message="name"></child>
    </div>
</template>
<script>
    import child from './child.vue';
    export default {
        components: {
            child
        },
        data() {
            return {
                name: 'linxin'
            }
        }
    }
</script>

在子組件 child.vue 中的 props 選項中聲明它期待得到的數據

<template>
    <span>Hello {{message}}</span>
</template>
<script>
    export default {
        // 在 props 中聲明獲取父組件的數據經過 message 傳過來
        props: ['message']
    }
</script>

那麼頁面中就會渲染出:Hello linxin

單向數據流

當父組件的 name 發生改變,子組件也會自動地更新視圖。可是在子組件中,咱們不要去修改 prop。若是你必需要修改到這些數據,你可使用如下方法:

方法一:把 prop 賦值給一個局部變量,而後須要修改的話就修改這個局部變量,而不影響 prop

export default {
    data(){
        return {
            newMessage: null
        } 
    },
    props: ['message'],
    created(){
        this.newMessage = this.message;
    }
}

方法二:在計算屬性中對 prop 進行處理

export default {
    props: ['message'],
    computed: {
        newMessage(){
            return this.newMessage + ' 哈哈哈';
        }
    }
}

自定義事件

prop 是單向綁定的:當父組件的屬性變化時,將傳導給子組件,可是不會反過來。修改子組件的 prop 值,是不會傳回給父組件去更新視圖的。那麼子組件要如何去與父組件通信呢?

那就是自定義事件。經過在父組件 $on(eventName) 監聽自定義事件,當子組件裏 $emit(eventName) 觸發該自定義事件的時候,父組件執行相應的操做。

好比在父組件中控制一個彈框子組件的顯示,在子組件中按下關閉以後,告訴父組件去隱藏它,而後父組件就執行操做隱藏彈框。

<template>
    <div class="app">
        // hide 爲自定義事件,名字能夠本身隨便起,不能有大寫字母,可使用短橫線
        // @hide 監聽子組件觸發 hide 事件,則會執行 hideDialog 方法
        <dialog :is-show="show" @hide="hideDialog"></dialog>
        <button @click="showDialog">顯示彈框</button>
    </div>
</template>
<script>
    import dialog from './dialog.vue';
    export default {
        components: { dialog },
        data() {
            return {
                show: false
            }
        },
        methods: {
            showDialog() {
                this.show = true;
            },
            hideDialog() {
                this.show = false;
            }
        }
    }
</script>

在子組件 dialog.vue 中:

<template>
    <div class="dialog" v-show="isShow">
        <p>這裏是彈框子組件</p>
        <button @click="toHide">關閉彈框</button>
    </div>
</template>
<script>
    export default {
        // 駝峯式命名的 prop 須要轉換爲相對應的短橫線隔開式 is-show
        props: ['isShow'],
        methods: {
            toHide(){
                // $emit 方法觸發父組件的監聽事件
                this.$emit('hide');
            }
        }
    }
</script>

這樣就實現了父子組件之間的相互通信。

Vuex

上面的例子都是創建在父子關係的組件上,可是對於其餘層級的關係,實現起來就比較繁瑣了。那麼這時候 Vuex 就能更好的幫你在各個組件間實時通信了。關於 Vuex,可查看個人另外一篇文章:Vuex 模塊化實現待辦事項的狀態管理

總結

組件通信並非必定要使用必需要使用 Vuex,對於一些簡單的數據傳遞,prop 也能夠完成。本文主要是對組件傳參的一些基礎知識點的記錄,實戰能夠參考 notepad 這個例子,使用 prop 實現子組件的顯示與隱藏,使用 vuex 來實現組件間的數據狀態管理。

更多文章:blog

相關文章
相關標籤/搜索