我有一个JavaScript对象,我试图绑定到Vue视图。Javascript对象数据绑定Vue
我正在运行一个函数来使用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>
可以确认 - [小提琴](https://jsfiddle.net/qfxpznor/) – ShadowScripter
感谢您的回复。这只是一个简单的例子,我放在一起,我不知道这将如何在我的实际代码中工作...让我测试一下,看看它是否会工作... – vmitchell85
这确实工作,但我必须循环通过每个条目来单独推送每个条目而不是仅使用全新的数据集更新对象 - 我也尝试设置整个对象,然后再推入一个条目,但它仍然不更新 - 如果我找不到更好的解决方案然后我将这个标记为答案(或者那是不好的做法?) – vmitchell85