【问题标题】:parse json in html from vue js从 vue js 解析 html 中的 json
【发布时间】:2018-05-08 11:20:37
【问题描述】:

我是 Vue js 的新手。

我正在尝试使用

将 json 解析到 html 表中
<tr v-for="(item) in products">
   <td>{{item.id}}</td>
</tr>

但表格打印空行。 如果我尝试打印

<td>{{item}}</td>

然后每行打印json的单个字符。

我的 json : "{id: 'mu'}"

here is the screenshot of the table that prints single character

我错了。拜托,一点指导会有所帮助。

var app4 = new Vue({
el: '#Itemlist',
data: {
    products: []
},

mounted: function (){
    var self = this;
    $.ajax ({
        url: "getAll",
        method: "GET",
        success: function (data) {
            self.products = "{id: 'mu'}";
        },
        error: function(error) {
            console.log(error)
        }
    });
}

})

【问题讨论】:

  • products变量需要是一个数组,所以[{id: 'mu'}]
  • @ChrisDixon 我也试过了,但现在又添加了 2 个空行。
  • 您将产品设置为字符串,而不是数组...我将添加答案。

标签: jquery html vue.js


【解决方案1】:
var app4 = new Vue({
el: '#Itemlist',
data: {
    products: []
},

mounted: function (){
    var self = this;
    $.ajax ({
        url: "getAll",
        method: "GET",
        success: function (data) {
            // this is your issue
            //self.products = "{id: 'mu'}";

            self.products.push({id: 'mu'});
        },
        error: function(error) {
            console.log(error)
        }
    });
}

})

【讨论】:

  • 没问题!乐于助人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
相关资源
最近更新 更多