1.Vue.js安装
- 方式一:直接CDN引入(你可以选择引入开发环境版本或生产环境版本)
- 方式二:下载和引入(学习模式时下载)
- 方式三:NPM安装
2.Vue.js初体验
01-HelloVuejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h2>{{message}}</h2>
<h1>{{name}}</h1>
</div>
<script src="../js/vue.js"></script>
<script>
//es6: let(定义变量)/const(定义常量)
//编程范式:声明式编程,以前js的做法的编程范式是:命令式编程
const app=new Vue({
el:"#app", //用于要挂载管理的元素
data:{ //定义数据
message:'你好啊,李欢迎',
name:'codewhy'
}
})
</script>
</body>
</html>
结果:
02-vue列表展示
v-for语句的使用:
< li v-for=“item in movies” > {{item}} < /li >
每次从movies数组中取出一个元素给item
<body>
<div id="app">
<ul>
<!-- 对谁遍历,对谁加v-for,每次从movies选一个给item,再显示item-->
<li v-for="item in movies">{{item}}</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app=new Vue({
el:'#app',
data:{
message:'你好啊',
movies:['星际穿越','大话西游','少年派','盗梦空间']
}
})
</script>
</body>
结果:
03-vue案例-计数器
v-on监听事件(例如监听点击事件,可写为v-on:click)
< button v-on:click=counter++ >+< /button >
<body>
<!-- v-on:click监听点击事件 -->
<div id="app">
<h2>当前计数:{{counter}}</h2>
<!-- <button v-on:click=counter++>+</button>
<button v-on:click=counter->-</button> -->
<button v-on:click="add">+</button>
<button v-on:click="sub">-</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app=new Vue({
el:'#app',
data:{
counter:0
},
methods:{ // methods: 是方法
add: function(){
console.log("add被执行");
this.counter++;
},
sub:function(){
console.log("sub被执行");
this.counter--;
}
}
})
</script>
</body>
结果:
3.Vue中的MVVM
4.创建Vue实例传入的options
上例中使用的有el、data、methods,有很多,具体了解官网
5.Vue的生命周期
事物从诞生到消亡的整个过程,一个new Vue() 其实是做了很多事情,可以看Vue的源码,下载源码到网址
6.定义vue的template(模板)
以vscode编译器为例
在html.json中将你要设置的模板代码输入进去,其中要注意的是各个转义字符的含义:
转义字符解释:
\t " \n都是转义字符,而空格就是单纯的空格,输入时可以输入空格
\t 的意思是 横向跳到下一制表符位置 等于 Tab键
" 的意思是 双引号
\n 的意思是回车换行
效果就是,你只要输入vue,再按下回车就可以出现以下代码:
7.mustache语法(双大括号{{}}语法)
响应式变化
<body>
<div id="app">
<h2>{{message}}</h2>
<h2>{{message}}, 李银河!</h2>
<h2>{{firstName + ' ' + lastName}}</h2>
<h2>{{firstName }} {{lastName}}</h2>
<h2>{{counter * 2}}</h2>
</div>
<script src="../js/vue.js"></script>`在这里插入代码片`
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
firstName: 'kobe',
lastName: 'bryant',
counter: 100
}
})
</script>
</body>
结果:
8.v-once
9.v-html
<body>
<div id="app">
<h2>{{url}}</h2>
<h2 v-html= 'url'>{{url}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message:'你好啊',
url: '<a href= "http://www.baidu.com">百度一下</a>'
},
methods: {}
});
</script>
</body>
结果:
10.v-text (用的很少)
<body>
<div id="app">
<h2>{{message}},李银河</h2>
<h2 v-text="message">,李银河</h2> //此时会覆盖‘李银河’,所以v-text不常用
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message:'你好啊'
},
methods: {}
});
</script>
</body>
11.v-pre (用的很少)
12.v-cloak
13.v-bind的基本使用
<body>
<div id="app">
<img v-bind:src="imgURL" alt="">
<a v-bind:href="aHref">百度一下</a>
<!-- 语法糖的写法(即简写方式) v-bind可以简写为: -->
<img :src="imgURL" alt="">
<a :href="aHref">百度一下</a>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
imgURL: 'https://img12.360buyimg.com/ceco/s300x300_jfs/t1/16689/15/3317/271321/5c25cc47E57baafdf/4c610e3d089d4616.jpg!q70.jpg.webp',
aHref: 'http://www.baidu.com'
},
methods: {}
});
</script>
</body>
结果:
14.v-bind的动态绑定class属性(对象语法)
<body>
<style>
.active{
color:red;
}
</style>
<div id="app">
<!-- {} 表示一个对象,里面有 键1:键值对 ,键2:键值对 (键加不加单引号都一样)等-->
<!-- <h2 :class="{key1: boolean, key2: boolean}">{{message}}</h2> -->
<!-- <h2 :class="{类名1: 值, 类名2: 值}">{{message}}</h2> -->
<h2 :class="{active: isActive, 'line': isLine}">{{message}}</h2>
<h2 :class="getClasss ()">{{message}}</h2>
<!-- <button v-on:click="btnClick ()">变色</button>,此时的方法()可以省略了-->
<button v-on:click="btnClick">变色</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
isActive: true,
isLine: true
},
methods: {
btnClick: function () {
this.isActive = !this.isActive
},
getClasss: function () {
return {active: this.isActive, 'line': this.isLine}
}
}
});
</script>
</body>
结果效果(点击按钮,文字将在红色和黑色来回切换)
15.v-bind的动态绑定class属性(数组语法)
<body>
<div id="app">
<h2 class="text" :class="['active','line']">{{message}}</h2>
<h2 class="text" :class="[active,line]">{{message}}</h2>
<!-- <h2 class="text" :class="getclasses()">{{message}}</h2> 这里的方法后的小括号不能省略 -->
<h2 class="text" :class="getclasses()">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
active: 'aaaaa',
line: 'bbbb'
},
methods: {
getclasses: function (){
return [this.active, this.line]
}
}
});
</script>
</body>
结果:
16.v-bind动态绑定style属性(对象语法)
<body>
<style></style>
<div id="app">
<!-- <h2 :style="{key(属性名): 属性值 }">{{message}}</h2> -->
<!-- '50px'必须加上单引号,否则是当做一个变量去解析-->
<!-- <h2 :style="{fontSize: '50px'}">{{message}}</h2> -->
<!-- finalSize当做一个变量使用 -->
<!-- <h2 :style="{fontSize: finalSize }">{{message}}</h2> -->
<!-- <h2 :style="{fontSize: finalSize + 'px',backgroundColor: finalColor}">{{message}}</h2> -->
<h2 :style="getStyles()">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
finalSize: 100,
finalColor: 'red'
},
methods: {
getStyles: function () {
return {fontSize: this.finalSize + 'px',backgroundColor: this.finalColor}
}
}
});
</script>
</body>
结果:
17.v-bind动态绑定style属性(数组语法)
<body>
<div id="app">
<h2 v-bind:style="[baseStyle,baseStyle2]">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
baseStyle: {backgroundColor: 'red'},
baseStyle2: {fontSize: '100px'}
},
methods: {}
});
</script>
</body>
结果:
18.计算属性(computed)的基本使用
<body>
<div id="app">
<h2>{{firstName + ' ' +lastName}}</h2>
<h2>{{firstName }} {{lastName}}</h2>
<h2>{{getFullName()}}</h2>
<h2>{{fullName}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
firstName: 'Lebron',
lastName: 'James'
},
//computed: 计算属性()
computed: {
fullName: function () {
return this.firstName + ' ' + this.lastName
}
},
methods: {
getFullName: function () {
return this.firstName + ' ' + this.lastName
}
}
});
</script>
</body>
结果:
19.计算属性(computed)的复杂操作
<body>
<div id="app">
<h2>总价格: {{totalPrice}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
books: [
{id: 110, name: 'Unix编程艺术', pirce: 119},
{id: 111, name: '代码大全', pirce: 105},
{id: 112, name: '深入理解计算机原理', pirce: 98},
{id: 110, name: '现代操作系统', pirce: 87},
]
},
computed: {
totalPrice: function () {
// es5
let result = 0
// for (let i=0; i<this.books.length; i++){
// result +=this.books[i].pirce
// }
// return result
// es6的第一种写法
for (let book of this.books) {
result +=book.pirce
}
return result
// es6的第二种写法
// for (let i in this.books) {
// result +=this.books[i].pirce
// }
// return result
}
},
methods: {}
});
</script>
</body>
结果