引言:
Vue是著名的前端框架,从今天开始要慢慢了解它,本节粗略介绍了Vue的基本知识点
先从一个Hello world开始吧
加油加油!!
Hello world
从Vue官网上下载Vue的源代码,初学我们可以直接用
1
| <script src="vue.js"></script>
|
我们可以直接将它放在一个单独的文件当中,然后放在head的位置,用script标签来引入,这样就可以使用vue了
这里是一个Hello world小实例
如果使用纯js的话
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> </title> </head> <body> <div id="app"></div> <script> var iApp = document.getElementById('app'); iApp.innerHTML='hello world'; setTimeout(function() { iApp.innerHTML = 'bye,world' }, 2000) </script> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="vue.js"></script> </head> <body> <div id="root">{{msg}}</div>
<script> let vm = new Vue({ el: '#root', data:{ msg: "hello world" } }) setTimeout(function() { app.$data.msg = 'bye,world' }, 2000) </script> </body> </html>
|
挂载点、模板之间的关系
挂载点
1 2 3 4 5 6 7 8 9 10 11 12 13
| <body> <!--挂载点、模板、实例的关系--> <div id="root">{{msg}}</div><!--这里是挂载点--> <div>{{msg}}</div><!--这里不是挂载点,所以页面内会直接显示出原本的内容--> <script> new Vue({ el: '#root', data: { msg: "hello world" } }) </script> </body>
|
模板
模板就是指挂载点内部的内容
1 2 3
| <div id="root"> <h1>hello {{msg}}</h1><!--模板:挂载点内部的内容--> </div>
|
还有另一种的表现方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <body> <!--挂载点、模板、实例的关系--> <div id="root"></div> <script> new Vue({ el: '#root', template:'<h1>hello {{msg}}</h1>', data: { msg: "hello world" } }) </script> </body>
|
数据的显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <body> <div id="root"> <h1>{{msg}}</h1> <h2 v-text="msg"></h2>
<h3 v-html="msg"></h3> <h4 v-text="content"></h4> <h4 v-html="content"></h4>
</div> <script> new Vue({ el: '#root', data: { msg: "hello world", content:"<h4>会不会转义</h4>" } }) </script> </body>
|
方法的添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <body> <div id="root"> <div v-on:click="handleClick">{{msg}}</div> <div @click="handleClick">{{msg}}</div> </div> <script> new Vue({ el: '#root', data: { msg: "hello", }, methods: { handleClick: function () { this.msg = 'world' } } }) </script> </body>
|
属性绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <body> <div id="root"> <div v-bind:title="title">hello world</div>
<div v-bind:title="'你好啊'+title">hello world</div> <div :title="'你好啊'+title">hello world</div>
</div> <script> new Vue({ el:"#root", data:{ title:"this is hello world" } }) </script> </body>
|
双向数据绑定
单向数据:数据决定页面的显示,但是页面无法决定数据里的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <body> <div id="root"> <div :title="title">hello world</div> <input :value="content"/> <div>{{content}}</div> </div> <script> new Vue({ el:"#root", data:{ title:"this is hello world", content:"this is content" } }) </script> </body>
|
双向数据:数据决定页面的显示,页面也可以决定数据里的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <body> <div id="root"> <div :title="title">hello world</div> <input v-model="content"/> <div>{{content}}</div> </div> <script> new Vue({ el:"#root", data:{ title:"this is hello world", content:"this is content" } }) </script> </body>
|
Vue中的计算属性和侦听器
计算属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <body> <div id="root"> 姓:<input v-model="firstName" /> 名:<input v-model="lastName" /> <div>{{firstName}}{{lastName}}</div> <div>{{fullName}}</div> </div> <script> new Vue({ el:"#root", data:{ firstName:'', lastName:'' }, computed:{ fullName:function () { return this.firstName +""+this.lastName } } }) </script> </body>
|
监听器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <body> <div id="root"> 姓:<input v-model="firstName" /> 名:<input v-model="lastName" /> <div>{{fullName}}</div> <div>{{count}}</div> </div> <script> new Vue({ el:"#root", data:{ firstName:'', lastName:'', count:0 }, computed:{ fullName:function () { return this.firstName +""+this.lastName } }, watch:{ fullName:function () { this.count++ } } }) </script> </body>
|
常见命令的使用
v-if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <body> <div id="root"> <div v-if="show">hello world</div> <button @click="handleClick">toggle</button> <ul> <li v-for="item of list">{{item}}</li> <li v-for="item of list" :key="item">{{item}}</li> <li v-for="(item,index) of list" :key="index">{{item}}</li> </ul> </div> <script> new Vue({ el:"#root", data:{ show:true, list:[1,2,3] }, methods:{ handleClick:function () { this.show=!this.show } }, }) </script> </body>
|