近來在專案裡用 Vue 取代繁瑣的 jQuery DOM 操作,體驗非常好,於是照著官網把表單控件的部分實作了一遍。以下程式直接存成 html 檔、在瀏覽器打開就能看到效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PlayAround2 Have Fun</title>
<script src="https://cdn.jsdelivr.net/vue/1.0.26/vue.min.js"></script>
<style>
h2{ text-decoration:underline; }
.red{ color:red; }
.green{ color:green; }
</style>
</head>
<body>
<div id="app">
<h2>checkBox</h2>
<input type="checkbox" v-model="checked">
<label>{{checked}}</label>
<h2>multi checkbox</h2>
<input type="checkbox" id="Kobe" value="Kobe" v-model="names">
<label for="Kobe">Kobe</label>
<input type="checkbox" id="Curry" value="Curry" v-model="names">
<label for="Curry">Curry</label>
<input type="checkbox" id="Aaron" value="Aaron" v-model="names">
<label for="Aaron">Aaron</label>
<br>
<span>Checked names: {{names | json}}</span>
<h2>Radio</h2>
<input type="radio" id="one" value="one" v-model="picked">
<label for="one">one</label><br>
<input type="radio" id="two" value="two" v-model="picked">
<label for="two">two</label><br>
<span>Picked: {{picked}}</span>
<h2>Select</h2>
<select v-model="selected">
<option selected>Kobe</option>
<option>Curry</option>
<option>Aaron</option>
</select>
<span>Selected: {{selected}}</span>
<h2>Multi Select</h2>
<select multiple v-model="multiSelected">
<option>Kobe</option>
<option>Curry</option>
<option>Aaron</option>
</select>
<span>Selected: {{multiSelected}}</span>
<h2>Select with v-for</h2>
<select v-model="selectedPlayer">
<option v-for="option in options" :value="option.value">{{option.text}}</option>
</select>
<span>Selected: {{selectedPlayer}}</span>
<h2>lazy —— 把更新時機從 input 改成 change</h2>
<input v-model="msg" lazy>
<span>Msg:{{msg}}</span>
<h2>number —— 自動轉型別</h2>
<input v-model="age" number>
<span>age:{{age}}</span>
<h2>debounce —— 延遲更新 view</h2>
<p>Edit me <input v-model="delayMsg" debounce="500"></p>
<span>delayMsg:{{delayMsg}}</span>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
checked: false,
names: [],
picked: "",
selected: "",
multiSelected: "",
options: [
{ text: "Kobe", value: "Bryant" },
{ text: "Stephen", value: "Curry" },
{ text: "Aaron", value: "Kong" }
],
selectedPlayer: "",
msg: "",
age: "",
delayMsg: ""
},
methods: {}
})
</script>
</body>
</html>
目前為止沒遇到明顯缺點,或許是深入還不夠。整體來說,用它處理表單的體驗相當順手。