20.VUE学习之-变异方法push的留言版实例讲解
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="hdcms">
<li v-for="v in comments">
{{v.content}}
</li>
<!--当textarea里的内容改变时,会动态改变data/current_content-->
<textarea v-model="current_content" cols="30" rows="10"></textarea><br>
<button v-on:click="push">发表</button>
</div>
<script>
var app = new Vue({
el: '#hdcms',
data: {
//当前用户输入内容
current_content:'',
comments: [
{content: '测试一'},
{content: '测试二'},
]
},
methods:{
push(){
//用data/current_content里动态改变的值,声明一条记录赋值给变量content
var content = {content:this.current_content}
//动态把content加入data/comments的数组里,动态的加入页面li列表里
this.comments.push(content);
//操作完后,把data/current_content里的值清空
this.current_content ='';
}
}
});
</script>
</body>
</html>
效果:
[Haima的博客]
http://www.cnblogs.com/haima/