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>
添加回答
举报