javascript - vue v-for ul 组件的数组数据如何动态赋值
黄舟
黄舟 2017-04-11 12:09:23
[JavaScript讨论组]

这是一个

//---模板--

  • {{ item.message }}

//注册 Vue.component('select-custom', { template: System.getTxt('./proPanel/selectUI.html'), data: function () { return { items: itemsPrivate }; } });

var items1 = [

    {message: 'Foo1' },
    {message: 'Bar1' }
];
var items2 =  [
    {message: 'Foo2' },
    {message: 'Bar2' }
];

希望 itemsPrivate数组 可以自由赋值使ul的内容 例如,itemsPrivate = items1 这样。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
阿神

简单的实现方式是:

Vue.set(this.$data,'items',items2)

还有一种方式:

// html
<li v-for="item in someItems">
    {{ item.message }}
</li>
// Vue
data:function(){
    return {
        itemsSource:"items1",
        itemsArray:{
            items1:[{message:'Foo1'},{message:'Bar1'}],
            items2:[{message:'Foo2'},{message:'Bar2'}]
        }
    };
},
computed:{
    someItems:{
        return this.itemsArray[this.itemsSource];
    }
}
怪我咯

查了好久,我这样实现的。之前对问题没有表达清楚。

 //调用
<select-custom id="select1" :selectedItems="['Foo1','Foo2']" ></select-custom>
<select-custom id="select2" :selectedItems="['Bar1','Bar2']" ></select-custom>
//模板
<p class="proPaneSelect">
    <button class="proPanelBtn" @click="toggleListHandler">设置表头<span>◢</span></button>
    <ul class="proPaneUl" style="display: block">
        <li v-for="item in items">
            {{ item }}
        </li>
    </ul>
</p>
//注册
 Vue.component('select-custom', {
        template: System.getTxt('./proPanel/selectUI.html'),
        data: function () {
            return {
                items: this.selectedItems
            };
        },
        props: {
            id: String,
            selectedItems:Array
        }
    });

PHPz

使用vue的set方法,
Vue.set(this.data,'items',items2)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号