簡體   English   中英

將數據從Vue實例傳遞到Vue組件

[英]Pass the data from Vue instance to the Vue component

如何將數據從Vue實例傳遞到Vue組件? 我正在嘗試對駐留在組件模板中的“ li”進行“ v-for”操作,這是小提琴

的HTML

<div id="app">
    <my-notification></my-notification>
</div>

<template id="my-template">
    <ul>
        <li v-for="nn in notifications">{{ nn }}</li>
    </ul>
</template>

Vue腳本

Vue.component('my-notification',{
    template : '#my-template',
});

new Vue({
    el : '#app',
    data : {
        notifications : ['notification 1','notification 2','notification 3']
    }
});

不幸的是,到目前為止,我已經嘗試了什么(請參見我的小提琴),但是沒有任何幫助嗎?

如果您的應用程序越來越大,您也可以考慮使用vuex

我更新了我的代碼

<div id="app">
    <my-notification :notification="notifications"></my-notification>
</div>

<template id="my-template">
    <ul>
        <li v-for="nn in nns">{{ nn }}</li>
    </ul>
</template>

Vue.component('my-notification',{
    template : '#my-template',
    props : ['notification'],
    data : function(){
        return {
            nns : this.notification
        }
    }
});

new Vue({
    el : '#app',
    data : {
        notifications : ['notification 1','notification 2','notification 3']
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM