【发布时间】:2017-06-07 14:04:05
【问题描述】:
我有一个非常简单的 Vue js 页面,如下所示:
<html>
<head>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="main-container">
<div id="contact-table">
<table>
<thead>
<tr>
<th>Contact Name</th>
<th>Email</th>
</tr>
</thead>
<tbody v-for="contact in contacts">
<tr v-on:click="selectContact(contact.index)">
<td>
{{contact.name}}
</td>
<td>
{{contact.email}}
</td>
</tr>
</tbody>
</table>
</div>
<div id="contact-form">
<input type="text" v-model="contactName"/>
<input type="text" v-model="contactEmail"/>
<button type="button" v-on:click="updateContact">Update Contact</button>
</div>
</div>
</body>
<script>
var hub = {
contacts: [
{
name: "James",
email: "Jame@bond.com",
index: 0
},
{
name: "Mary",
email: "Mary@lamb.com",
index: 1
}
],
index: 0
};
var contactTable = new Vue({
el: "#contact-table",
data: {
contacts: hub.contacts
},
methods: {
selectContact: function(index) {
hub.index = index;
console.log(hub.index);
}
}
});
var contactForm = new Vue({
el: "#contact-form",
data: {
contactName: hub.contacts[hub.index].name,
contactEmail: hub.contacts[hub.index].email
},
methods: {
updateContact: function() {
hub.contacts[hub.index].name = this.contactName;
hub.contacts[hub.index].email = this.contactEmail;
}
}
});
</script>
</html>
基本上它包含两个 Vue 部分——表格和输入表单。通过单击表中的行,它应该更改输入表单中的值,但单击更新联系人按钮,它应该更新表单详细信息。
第二部分运行良好 - 但是,当我单击表格的行时,虽然 console.log 告诉我 hub.index 已更新好,但输入表单中的视图却没有。
我相信这应该是双向绑定,所以我只是想知道我在哪里做错了。
【问题讨论】:
-
听起来您可能想要实现event-bus 或state-management pattern
标签: javascript vue.js