1
我誤解如何更新組件。 所以,這裏是HTML:更新組件
<div id="app">
<form v-on:submit="submitForm">
<input type="text" id="txtSearch">
<input type="submit" value="go">
</form>
<br><br>
<user></user>
</div>
而且JS:
let userComponent = {
template: 'your name is : {{name}}<br>You are {{age}}'
};
let vueApp = new Vue({
el: '#app',
components: {'user': userComponent},
methods: {
submitForm: function(e) {
e.preventDefault();
let val = document.getElementById('txtSearch').value;
alert('submitted : ' + val);
// normally i do a search here : let result = mySearch(val);
// but let's do the job with this :
let result = {name: 'John', age: 27};
// so now, how to modify the <user> with this data result ?
}
}
});
所以,我的目標是創建一個模板,當然更新他的數據。 如何做到這一點? 我創建了一個jsfiddle進行測試:https://jsfiddle.net/4w0kh30t/1/ 感謝您的幫助。
你的問題和這個[SO問題]非常相似(https://stackoverflow.com/questions/42694457/getting-form-data-on-submit) 。你可以像這樣[小提琴](https://jsfiddle.net/awolf2904/4w0kh30t/4/)。 – AWolf