楼主可以多读读Vue的官方文档,深入点理解数据绑定的概念就好了,直接上示例吧:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Vue</title>
<meta charset="utf-8">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div>投票</div>
<template v-for="(item, index) in options">
<input type="radio" name="vote" :value="item" v-model="vote">
<label for="vote">选项{{ index }}</label>
</template>
<div>
<button @click="newOption">增加选项</button>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
options: [
'1',
'2'
],
vote: '1'
},
methods: {
newOption() {
this.options.push(this.options.length.toString());
}
}
})
</script>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…