【发布时间】:2019-06-11 22:28:05
【问题描述】:
我有一个由发布请求(称为get_teams 的方法)触发的返回数据,它没有传递给我的 vue 模板。谁能告诉我我做错了什么?我没有将数据正确绑定到我的 vue 模板吗?
app.js
var app = new Vue({
el: '#app',
components: {
teamsTableTemplate
},
data: {
teams: null,
editable: true,
show_teams: false
},
methods: {
get_teams : function(){
this.reset_show('get_teams')
$.post(js_local.ajaxurl,{
action:'get_advisor_teams'
}).done(function(data){
this.teams = data
this.show_teams = true
console.log(this.teams)
}).fail(function(data){
console.log('fail @ { action : get_advisory_teams }')
})
}
}
})
teams-table-template.js
const teamsTableTemplate = {
template:
`
<table class="table tablesorter">
<thead class="text-primary">
<tr></tr>
</thead>
<tbody class="">
<tr v-for="team in teams">
<td>
<div class="form-check">
<label for="69cd1dbb353338" class="form-check-label">
<input id="69cd1dbb353338" type="checkbox" class="form-check-input"><span class="form-check-sign"></span>
<!---->
</label>
</div>
</td>
<td>
<p class="title">{{team.team_name}}</p>
<p class="text-muted">{{team.problem_statement_text}}</p>
</td>
<td class="td-actions text-right">
<button type="button" class="btn btn-link">
<!----><i class="tim-icons icon-pencil"></i></button>
</td>
</tr>
</tbody>
</table>
`,
props: ['teams','edit'],
data() {
return {
}
}
}
HTML
<div id="app">
<button @click="get_teams"></button>
<teams-table-template :teams="teams" :edit="editable" v-if="show_teams" />
</div>
【问题讨论】:
-
找不到您在组件中调用
get_teams方法的位置...请创建一个工作的codepen 或类似的。它将帮助我们帮助您。 -
如果您对 vue 有任何想法,那么我必须告诉您,如果您想使用来自 api 的值,则模板的反应性取决于数据属性或计算属性,请改用 vuex 并存储您想在 store 中使用的值,或者如果您只想从模板调用 api 并存储响应,然后在模板的 data 属性中创建一个局部变量并使用它,它将是响应式的。
标签: javascript html vue.js vuejs2 vue-component