2017-06-24 47 views
0

在这里,我填的阵列打印值localStorage无法从对象

let storageObj = {}; 
storageObj.date = (new Date().toLocaleString().replace(", ","T")); 
storageObj.url = this.responseURL; 
localStorage.setItem(storageObj.date, storageObj.url); 

mounted()我是从localStorage像遍历所有的数据:

for(let obj of Object.entries(localStorage)) 
{ 
    this.lastpasts.push(obj); 
} 

而且每个配售对象对lastpasts (位于data() {lastpasts : []})。

在模板我只想url打印:

<div class="ui divided items" v-for="el in lastpasts"> 
    <div class="item"> 
    {{el.url}} 
    </div> 
</div> 

但这种代码不打印什么。仅工作{{el}}。它的打印在HTML块:

[ "24.06.2017T11:55:10", "362e9cc5-e7e6" ] 
[ "24.06.2017T12:26:47", "b0f9f20d-7851" ] 

浏览器控制台没有任何错误。

+0

能否请你放的'的console.log(EL)的结果' ? –

+0

'的ReferenceError:1:13' –

+0

噢,对不起EL不是在定义。我的错。 'console.log(lastpasts)'(或lastposts?) –

回答

3

在聊天会话,我们设法解决他的问题。

它是由这样的事实,导致他在用数组的数组,而不是对象的数组。因为数组只能用于索引,而不是字段名称,所以{{el.url}}不起作用。

的代码从LocalStorage得到的值,就必须改变到:

mounted() { 
    for(let obj of Object.entries(localStorage)) { 
    var x = {}; 
    x.url = obj[0]; 
    x.date = obj[1]; 
    this.lastpasts.push(x);  
    } 
} 

现在,它是可以使用{{el.url}}

+0

哎呀,我会删除我的答案因为它相同,为你+1 +1 –

+0

欢呼声。当你写下你的答案时,我们正在聊天和解决它:) –