对于数组,vue.js good使用默认的数组方法: push,pop,shift,reverse...但是如何在对象中创建新项并粘贴一些值呢?
new Vue({
el: '#demo',
data: {
items: [
{
start: '12:15',
end: '13:15',
name: 'Whatch Vue.js Laracast',
description: 'Watched the Laracast series on Vue.js',
tags: ['learning', 'Vue.js', 'Laracast', 'PHP'],
note: "Vue.js is really awesome. Thanks Evan You!!!"
},
{
start: '13:15',
end: '13:30',
name: "Rubik's Cube",
description: "Play with my Rubik's Cube",
tags: ['Logic', 'Puzzle', "Rubik's Cube"],
note: "Learned a new algorithm."
}
],
},
methods: {
addItemAttribute: function(name, text) {
this.items[0][name] = text;
}
}
});
发布于 2016-06-25 09:11:20
您可以向Vue对象添加类似于普通javascript对象的属性。参考:https://vuejs.org/api/#Options-Data
var obj = { a: 1};
obj.a // 1
obj.b // undefined
obj.b = 2;
// Now obj.b is defined
obj.b // 2您可以像这样从Vue对象访问数据
var vm = new Vue({
data: {
a: 1,
b: 2
}
});现在我们可以从$data属性访问数据了
vm.$data.a === 1;
// You can add a new property
vm.$data.c = 3;
// it can be any object
vm.$data.d = {id: 1, name: 'ABC'}您可以使用component.options.props获取和更新组件的props
var component = Vue.component('props-demo-advanced', {
props: {
// just type check
size: Number,
// type check plus other validations
name: {
type: String,
required: true,
// warn if not two way bound
twoWay: true
}
}
});比方说,我们希望将name twoWay绑定为false
component.options.props.name.twoWay = faslse或者,您甚至可以更改整个对象。
component.options.props = {
size: Number,
type: {
type: Number,
required: false,
twoWay: false
}}
我不知道上面的配置是否正确,我只是想传达一下我的想法。
你应该先学习Javascript,对象是如何制作的,如何改变它们等等。
https://stackoverflow.com/questions/38023894
复制相似问题