【问题标题】:Pass the data from Vue instance to the Vue component将数据从 Vue 实例传递到 Vue 组件
【发布时间】:2017-02-27 08:47:05
【问题描述】:

如何将数据从 Vue 实例传递到 Vue 组件?我正在尝试对驻留在我的组件模板中的“li”执行“v-for”,这是fiddle

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']
    }
});

不幸的是,到目前为止我尝试了什么(见我的小提琴)都不起作用,请帮忙?

【问题讨论】:

标签: javascript vue.js vue-component


【解决方案1】:

如果你的应用变大,你也可以考虑vuex

【讨论】:

    【解决方案2】:

    我更新了我的代码

    <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']
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 1970-01-01
      • 2017-09-12
      • 2017-02-24
      • 2019-05-05
      • 2017-12-04
      • 1970-01-01
      • 2018-02-02
      相关资源
      最近更新 更多