Обычно директива v-for
имеет вид v-for="item in items"
, где items
- путь данных к массиву или объекту.
Путь данных к assigned_projects[]
равно info.response.data.user.assigned_projects
, поэтому v-for
будет:
<tr v-for="project in info.response.data.user.assigned_projects" :key="project.id">
<th>{{ project.name }}</th>
<th>{{ project.id }}</th>
</tr>
new Vue({
el: '#app',
data() {
return {
info: null
}
},
mounted() {
this.info = {
"response":{
"error":{
"error_code":0,
"error_msg":"",
"msg":""
},
"data":{
"user":{
"id":1,
"email":"email@address.com",
"first_name":null,
"last_name":null,
"photo_url":null,
"organisation":null,
"own_projects":[
],
"assigned_projects":[
{
"id":10,
"name":"test project",
"description":"cool stuff",
"image_url":"http://image_url",
"timezone":"UTC",
"owner":15,
"created_by":15,
"created_at":"2019-02-26 16:37:03",
"updated_at":"2019-02-26 16:37:03",
"pivot":{
"user_id":1,
"project_id":10
}
},
{
"id":8,
"name":"test project 2",
"description":"",
"image_url":"",
"timezone":"UTC",
"owner":15,
"created_by":15,
"created_at":"2019-02-21 18:36:55",
"updated_at":"2019-02-21 18:36:55",
"pivot":{
"user_id":1,
"project_id":8
}
}
]
}
}
}
};
}
})
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tfoot th {
font-weight: normal;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
<script src="https://unpkg.com/vue@2.6.8"></script>
<div id="app">
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Type</th>
</tr>
</thead>
<tfoot>
<tr v-for="project in info.response.data.user.assigned_projects" :key="project.id">
<th>{{ project.name }}</th>
<th>{{ project.id }}</th>
</tr>
</tfoot>
</table>
</div>
Чтобы упростить ваш шаблон для удобства чтения, здесь следует рассмотреть вычисляемое свойство :
// template
<tr v-for="project in projects" :key="project.id">...</tr>
// script
computed: {
projects() {
// empty array if `this.info` is not yet defined
return this.info && this.info.response.data.user.assigned_projects || [];
}
}
new Vue({
el: '#app',
data() {
return {
info: null
}
},
computed: {
projects() {
return this.info && this.info.response.data.user.assigned_projects || [];
}
},
mounted() {
this.info = {
"response":{
"error":{
"error_code":0,
"error_msg":"",
"msg":""
},
"data":{
"user":{
"id":1,
"email":"email@address.com",
"first_name":null,
"last_name":null,
"photo_url":null,
"organisation":null,
"own_projects":[
],
"assigned_projects":[
{
"id":10,
"name":"test project",
"description":"cool stuff",
"image_url":"http://image_url",
"timezone":"UTC",
"owner":15,
"created_by":15,
"created_at":"2019-02-26 16:37:03",
"updated_at":"2019-02-26 16:37:03",
"pivot":{
"user_id":1,
"project_id":10
}
},
{
"id":8,
"name":"test project 2",
"description":"",
"image_url":"",
"timezone":"UTC",
"owner":15,
"created_by":15,
"created_at":"2019-02-21 18:36:55",
"updated_at":"2019-02-21 18:36:55",
"pivot":{
"user_id":1,
"project_id":8
}
}
]
}
}
}
};
}
})
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tfoot th {
font-weight: normal;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
<script src="https://unpkg.com/vue@2.6.8"></script>
<div id="app">
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Type</th>
</tr>
</thead>
<tfoot>
<tr v-for="project in projects" :key="project.id">
<th>{{ project.name }}</th>
<th>{{ project.id }}</th>
</tr>
</tfoot>
</table>
</div>