【发布时间】:2016-04-23 17:40:31
【问题描述】:
我有一个试图绑定到 Vue 视图的 JavaScript 对象。
我正在运行一个函数来使用 AJAX 更新 JavaScript 对象,我希望 Vue 绑定到 JS 对象并在对象更新时更新视图,尽管这并没有发生。
研究建议在 Vue 声明中进行 AJAX 调用,但由于其他限制,我宁愿不这样做。
我创建了一个小提琴来说明问题所在,因为它可以在没有 AJAX 部分的情况下重现,并且粘贴了下面的代码。
https://jsfiddle.net/g6u2tph7/5/
提前感谢您的时间和智慧。
谢谢,
vmitchell85
JavaScript
window.changeTheData = function (){
externalJSSystems = [{description: 'Baz'}, {description: 'Car'}];
document.getElementById("log").innerHTML = 'function has ran...';
// This doesn't update the Vue data
}
var externalJSSystems = [{description: 'Foo'}, {description: 'Bar'}];
Vue.component('systable', {
template: '#sysTable-template',
data() {
return {
systems: externalJSSystems
};
}
});
new Vue({
el: 'body'
});
HTML
<systable :systems="systems"></systable>
<button type="button" onclick="changeTheData()">Change</button>
<br><br>
<div id="log"></div>
<template id="sysTable-template">
<table>
<thead>
<tr>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr v-for="sys in systems">
<td>{{ sys.description }}</td>
</tr>
</tbody>
</table>
</template>
【问题讨论】:
标签: javascript ajax vue.js