1 回答

TA貢獻1816條經驗 獲得超4個贊
從綁定中刪除括號,并從輸入中刪除除和[ ]之外的所有屬性:typev-model
<button @click="swapInputFields">Swap</button>
<input type="text" v-model="fromCity"><br>
<input type="text" v-model="toCity">
您只需要這些模型變量fromCity和toCity數據:
data() {
return {
fromCity:'FROM',
toCity :'TO'
}
},
并像這樣交換它們:
swapInputFields()
{
const temp = this.fromCity;
this.fromCity = this.toCity;
this.toCity = temp;
}
這是一個演示:
new Vue({
el: "#app",
data(){
return {
fromCity:'FROM',
toCity :'TO'
}
},
methods: {
swapInputFields()
{
const temp = this.fromCity;
this.fromCity = this.toCity;
this.toCity = temp;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="swapInputFields">Swap</button>
<input type="text" v-model="fromCity"><br>
<input type="text" v-model="toCity">
</div>
添加回答
舉報