【发布时间】:2017-12-16 15:45:40
【问题描述】:
我有一个这样的 api,每个都返回一个 json 对象列表
/api/data/foo
/api/data/bar
/api/data/fizz
我还有一个单页应用程序,其中包含一个表格和一个下拉选择器。
<select v-model="tableChoice">
<option selected>Foo</option>
<option>Bar</option>
<option>Fizz</option>
</select>
<table class="table table-hover">
<thead>
<tr>
<th v-for="header in tableHeaders">
{{ header }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="record in tableData" :key="record.recordId">
<td v-for="element in record">{{element}}</td>
</tr>
</tbody>
</table>
目前我只有 3 个像这样的独立 Vue,每个都有自己的 API url 和一组表头。
var fooLink = 'api/foo';
new Vue ({
el: '#app',
data: {
tableHeaders:["Record ID","Record Name", "Record Detail"],
tableData: null,
dataChoice: 'Foo'
},
methods:{
getFooData: function(){
this.$http.get(fooLink).then(function(response){
this.tableData = response.data;
}, function(error){
console.log(error.statusText);
});
}
},
mounted: function () {
this.getFooData();
}
});
如何使用单个表或组件,并根据 tableChoice 变量确定用于填充表数据的 API url?
关于组件的文档对使用道具进行条件加载并不清楚。
【问题讨论】:
标签: javascript vue.js vuejs2