0

我正在学习如何使用 Vue-Resource,并且我正在使用我正在使用的 api 碰壁。我正在尝试使用v-for: thread in threads.

这是我正在使用的示例:

[{
"threads": [{
    "no": 1,
    "name": "John",
    "com": "comment"
},{
    "no": 2,
    "name": "Jim",
    "com": "comment"
},{
    "no": 3,
    "name": "Jane",
    "com": "comment"
}],
"page": 0
}, {
"threads": [{
    "no": 4,
    "name": "Tom",
    "com": "comment"
},{
    "no": 5,
    "name": "Mark",
    "com": "comment"
}],
"page": 1
}]

该 api 在几页上列出了线程,我想要的是一个看起来像{{ thread.name }}{{ thread.com }}每个页面的列表,no但我并不完全清楚如何使用 vue-resource 获取该信息。目前,如果我使用

<li v-for="thread in threads"> {{ thread.name }}{{ thread.com }} </li>

我只是得到一个空项目列表。

4

1 回答 1

0

首先,我在名称值周围添加了双引号,并将您的 JSON 保存到名为 pages 的变量中。

var pages=[
  {
    "threads": [
      {
        "no": 1,
        "name": "John",
        "com": "comment"
      },
      {
        "no": 2,
        "name": "Jim",
        "com": "comment"
      },
      {
        "no": 3,
        "name": "Jane",
        "com": "comment"
      }
    ],
    "page": 0
  },
  {
    "threads": [
      {
        "no": 4,
        "name": "Tom",
        "com": "comment"
      },{
        "no": 5,
        "name": "Mark",
        "com": "comment"
      }
    ],
    "page": 1
  }
]

然后你的 HTML 可能看起来像这样

<div v-for:"page in pages">
  <div class="page" v-for"thread in page.threads">
    <div>{{thread.name}}{{thread.com}}</div>
  </div>
</div>
于 2016-08-18T17:41:18.850 回答