[英]How to get object from array in vue.js
I would like to select object from array in Vue.js : 我想从Vue.js中的数组中选择对象:
On pageload, the selectTitle() method is called. 在页面加载时,将调用selectTitle()方法。 I just want to select object (for example i=2) in my array 'titleList'.
我只想在数组“ titleList”中选择对象(例如i = 2)。 But for now I just get the Observer in return.
但是现在我只得到观察者作为回报。 I know it's about kind of scope or bind but I'm really new in vue (and js !) so someone could help me ?
我知道这是关于范围或绑定的,但是我在vue(和js!)中确实很新,所以有人可以帮助我吗?
Thanks ! 谢谢 !
var vm = new Vue({
el: '#titles',
data: {
titleList: [
{ title: 'Title1', details: 'details1', imgLocation:'', text: 'Lorem ipsum dolor sit amet.' },
{ title: 'Title2', details: 'details2', imgLocation:'', text: 'Lorem ipsum dolor sit amet.' },
{ title: 'Title3', details: 'details3', imgLocation:'', text: 'Lorem ipsum dolor sit amet.' },
{ title: 'Title4', details: 'details4', imgLocation:'', text: 'Lorem ipsum dolor sit amet.' },
{ title: 'Title5', details: 'details5', imgLocation:'', text: 'Lorem ipsum dolor sit amet.' }
],
},
mounted: function () {
this.setTimer();
this.selectTitle();
},
methods: {
selectTitle() {
i = 2;
let currentTitle = this.titleList[i];
console.log(i, currentTitle);
return currentTitle;
},
That's perfectly normal and exactly what you want to happen. 那完全是正常现象,而这正是您想要发生的事情。 Vue wraps every object into an observable automatically for you, so that when your data changes, all data-bindings in view update automatically without you having to do anything.
Vue会自动为您将每个对象包装到一个可观察对象中,以便在您的数据更改时,视图中的所有数据绑定都将自动更新,而您无需执行任何操作。 Don't worry about it, this works as a proxy, you can manipulate this object just normally.
不用担心,它可以作为代理,您可以正常操作该对象。 For example:
例如:
currentTitle.title = 'Changed title'
Will update the correct attribute and if you have a reference in your view, even update your view automatically without you having to worry about anything. 将更新正确的属性,并且如果您的视图中有引用,甚至可以自动更新视图,而不必担心任何事情。 That's the great thing about Vue.
Vue很棒。
Here's a codepen example taking your code a bit further, hope that helps understanding: Codepen Example 这是一个Codepen示例,使您的代码更进一步,希望有助于理解: Codepen示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.