VUE-来源bilibili
04、简单Vue创建实例
总结:
(1)定义变量:let 定义变量 ; const 定义常量;
(2)const app = new Vue ({
el:加上绑定的元素;
data:{ }
})
其中创建实例关键字为new,Vue指调用vue.js中的Vue函数直接调用函数进行创建实例,类似于var
app= function Vue();
<div id="app">
<p>{{message}}</p>
<p>{{name}}</p>
</div>
//适合数据,代码分离
<script src="../js/vue.js"></script> <!-- 引入vue.js -->
<script>
// let 变量 const 常量
//对象类型
const app = new Vue ({
el:'#app', //用于挂载管理的元素
data: {
message: 'hello world',//定义数据
name:'anna'
}
})
</script>
05、(掌握)Vue列表的展示
指令:v-for 可循环遍历数组
语法 v-for=“item in array”
<body>
<div id="app">
//vue实例管理者app的数据,通过v-for="item in array"来遍历数组进行展示
<ul>
<li v-for="item in movies">{{item}}</li>
</ul>
</div>
//引入vue
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
movies: ['海贼王','奶爸','超越','大话西游'],
}
})
</script>
</body>
06、(掌握)计数器实现
目的:编写一个计数器,实现加减并输出操作
先编写大概的结构
<div id="app">
<h2>当前计数:{{counter}}</h2>
<button>+</button>
<button>-</button>
</div>
为了监听按钮,绑定事件,使用指令v-on,因为是click事件配置counter的加减,所以为v-on:click=counter++
<div id="app">
<h2>当前计数:{{counter}}</h2>
<button v-on:click="counter++">+</button>
<button v-on:click="counter--">-</button>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
counter:0
}
})
</script>
另外,使用语法糖可简写v-on:click=“couter++” 为@click=“counter++”,效果一样,把v_on 变成了**@** 表示
<button @click="counter--">-</button>
此时已经能够实习加减操作了,但是不能满足复杂的操作,需要运用函数才能处理,这里用函数加上了输出的功能
使用methods(方法)去定义,这里的this,因为在同一对象app里,所以this.counter就是app.counter,如果直接使用counter++是会影响全局的counter
<body>
<div id="app">
<h2>当前计数:{{counter}}</h2>
<button v-on:click="add">+</button>
<button @click="sub">-</button>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
counter:0
},
methods:{
add: function(){
console.log('add'),
this.counter++
},
sub: function(){
console.log('sub'),
this.counter--
}
}
})
</script>
</body>
07(理解)vue的MVVM
维基百科MVVM
当有数据绑定时,Viewmodel会自动把数据绑定到view上
所以也可以这样单独把model提取出来,由于有内部代理,所以obj的值都赋予到了实例里的data了
08、(理解)vue的options对象
如何区别函数和对象
函数:function
方法:method
在类里面的叫方法,与实例相挂钩(面向对象)
09、(理解)vue的生命周期
vue生命函数详解
生命周期:事物从开始到消亡的过程
created 钩子可以用来在一个实例被创建之后执行代码:
const app = new Vue({
el: '#app',
data:obj,
methods:{
add: function(){
console.log('add'),
this.counter++
},
sub: function(){
console.log('sub'),
this.counter--
}
},
created: function(){
console.log ('created');
}
})
vue源码下载
查看new Vue的源码,先进到这个index.js
再进到.\instance\index.js
在这里回调了created函数,代表生命周期已经到了这里
10、(理解)生命周期函数有哪些
mounted、updated 和 destroyed等
12、插值操作-mustache语法
<body>
<div id="app">
<h2>{{message}}</h2>
<h2>{{message}},xxx</h2>
<!-- mustache不仅可以直接写变量,也可以写见简单阿达表达式-->
<h2>{{firstname + lastaname}}</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>
效果:
13、插值操作-其他指令使用
(1)v-once
<body>
<div id="app">
<h2>{{message}}</h2>
<h2 v-once>{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好'
}
})
</script>
</body>
效果:当改变message的值时,加了v-once指令的不会被修改
(2)v-html
希望请求服务器的链接,如果服务器返回的是带a标签的链接
想要把链接直接展示到页面上
<body>
<div id="app">
<h2>{{url}}</h2>
<h2 v-html="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>'
}
})
</script>
</body>
将链接格式化显示
(3)v-text
功能类似与mustache语法一样,用于显示;
这里注意,v-text会覆盖掉这之后的东西,所以一般很少使用
<body>
<div id="app">
<h2>{{message}},xxx</h2>
<h2 v-text="message">,xxx</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
}
})
</script>
</body>
(4)v-pre
v-pre会不解析,直接显示内容
<body>
<div id="app">
<h2>{{message}}</h2>
<h2 v-pre>{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
}
})
</script>
</body>
(5)v-cloak,可用于解决插值闪烁问题
先手动模拟延迟,因为代码是由上到下解析的
<body>
<div id="app">
<h2>{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
setTimeout(function(){
const app = new Vue({
el: '#app',
data:{
message:'你好',
}
})
},2000)
</script>
</body>
先出现如图,先渲染{{message}},两秒后解析的js代码执行,渲染完成才出来内容,导致加载会出现闪烁
<!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>v-cloak</title>
<style>
/* 根据有没有属性进行样式的显示,
当有这个属性时不显示,则不显示,
没有这个属性时就会直接显示内容,
就不会出现插值闪烁的问题*/
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="app">
<h2 v-cloak>{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
// vue解析前有一个属性 v-cloak
// vue解析后就没有了属性 v-cloak
setTimeout(function(){
const app = new Vue({
el: '#app',
data:{
message:'你好',
}
})
},2000)
</script>
</body>
</html>
14、掌握 绑定属性
(1)v-bind的基本使用
网站上的图片一般都是从服务器请求,不会写死
<body>
<div id="app">
<img src="{{imgurl}}">
<img v-bind:src="imgurl" >
<a v-bind:href="hrefurl">百度一下</a>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
imgurl:'https://i0.hdslb.com/bfs/sycp/creative_img/202102/846d523bc004837160b1bd06c64dc8f4.jpg@880w_388h_1c_95q',
hrefurl:'http://www.baidu.com'
}
})
</script>
</body>
效果:
v-bind可以使用语法糖写法,如下:
<div id="app">
<img :src="imgurl" >
<a :href="hrefurl">百度一下</a>
</div>
(2)v-bind动态绑定class(1)
<body>
<div id="app">
<h2 :class='active'>{{message}}</h2>
<h2 v-bind:class='active'>{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
active:'active'
},
})
</script>
</body>
效果:
15、掌握 v-bind动态绑定class(对象语法)
通过按钮来控制字体颜色变化
<!-- 大括号跟上对象,可写键值对 -->
<h2 v-bind:class={key1:value1,key2:value2}>{{message}}</h2>
key一般为类名,值一般为布尔值
<!-- 当布尔值为true时,添加class为类名1.类名2同理,后续通过控制布尔值来控制类名-->
<h2 v-bind:class={类名1:boolean,类名2:boolean}>{{message}}</h2>
<h2 v-bind:class="{active:true,line:true}">{{message}}</h2>
可以看到为true就可以添加进class
将布尔值放到data里进行控制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-bind(3)</title>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- <h2 :class='active'>{{message}}</h2>
<h2 v-bind:class='active'>{{message}}</h2> -->
<!-- 大括号跟上对象,可写键值对 -->
<!-- <h2 v-bind:class={key1:value1,key2:value2}>{{message}}</h2> -->
<!-- 当布尔值为true时,添加class为类名1.类名2同理,后续通过控制布尔值来控制类名-->
<!-- <h2 v-bind:class={类名1:boolean,类名2:boolean}>{{message}}</h2> -->
<h2 v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
isactive:'true',
isline:'false'
},
})
</script>
</body>
</html>
增加按钮控制布尔值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-bind(3)</title>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- <h2 :class='active'>{{message}}</h2>
<h2 v-bind:class='active'>{{message}}</h2> -->
<!-- 大括号跟上对象,可写键值对 -->
<!-- <h2 v-bind:class={key1:value1,key2:value2}>{{message}}</h2> -->
<!-- 当布尔值为true时,添加class为类名1.类名2同理,后续通过控制布尔值来控制类名-->
<!-- <h2 v-bind:class={类名1:boolean,类名2:boolean}>{{message}}</h2> -->
<h2 v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
<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:'false'
},
methods:{
btnclick:function(){
this.isactive = !this.isactive
}
}
})
</script>
</body>
</html>
效果
<h2 class='title',v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
并且这里的class可以合并,所以当有固定的样式时可以直接写,如果是会改变的class则用data进行控制
扩展:
<h2 class='title',v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
可缩写为如下,此时getclass()是一个计算属性
<h2 class='title' v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
<!-- 缩写 -->
<h2 class='title' v-bind:class="getclass()">{{message}}</h2>
<button v-on:click='btnclick'>点击按钮控制布尔值更换颜色</button>
并且在方法中添加对应的函数,这里的this是因为使用了app中data的数据
methods:{
btnclick:function(){
this.isactive = !this.isactive
},
getclass:function(){
return {active:this.isactive,line:this.isline}
}
}
16、掌握 v-bind动态绑定class(数组语法)
待补充
17、总结+作业
18、 掌握 v-bind动态绑定style(对象语法)
可利用v-bind:style来绑定一些内联样式
<!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 :style="{key:vlaue}">{{message}}</h2> -->
<!-- <h2 :style="{属性名:属性值}">{{message}}</h2> -->
<!-- 50px必须加上单引号,否则会当作变量解析 -->
<!-- <h2 :style="{fonSize: '50px'}">{{message}}</h2> -->
<h2 :style="{fontSize:finalsize + 'px',color:finalcolor}">{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
finalsize: 100,
finalcolor: 'red',
}
})
</script>
</body>
</html>
效果:
也可以单独把对象提取到函数中:
<body>
<div id="app">
<!-- <h2 :style="{key:vlaue}">{{message}}</h2> -->
<!-- <h2 :style="{属性名:属性值}">{{message}}</h2> -->
<!-- 50px必须加上单引号,否则会当作变量解析 -->
<!-- <h2 :style="{fonSize: '50px'}">{{message}}</h2> -->
<h2 :style="{fontSize:finalsize + 'px',color:finalcolor}">{{message}}</h2>
<h2 :style=getstyle()>{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
finalsize: 100,
finalcolor: 'red',
},
methods:{
getstyle:function(){
return {
fontSize:this.finalsize + 'px',color:this.finalcolor
}
}
}
})
</script>
</body>
19 掌握 v-bind动态绑定style(数组语法)
将对象提取出来作为数组中的一个
<body>
<div id="app">
<h2 :style="[baseStyle,baseStyle1]">{{message}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
message:'你好',
baseStyle:{color:'red'},
baseStyle1:{fontSize:'100px'},
}
})
</script>
</body>
20、掌握-计算属性的基本使用
<body>
<div id="app">
要显示lv ruohan 的效果
<!-- 第一种 -->
<h2>{{firstname+' '+lastname}}</h2>
<!-- 第二种-->
<h2>{{firstname}} {{lastname}}</h2>
<!-- 第3种 方法的形式-->
<h2>{{getfullname()}}</h2>
<!-- 第4种 计算属性,实际是个属性值-->
<h2>{{fullname}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data:{
firstname:'lv',
lastname:'ruohan'
},
computed:{
fullname:function(){
return this.firstname + ' ' + this.lastname
}
},
methods:{
getfullname:function(){
return this.firstname + ' ' + this.lastname
}
}
})
</script>
</body>
21、掌握-计算属性的复杂操作
自动计算书本总和
// methods比较适合循环输出,调几次就几次,没有缓存
// 计算属性在多次调用时只调用一次 ,有缓存,节省性能
<!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>v-text</title>
</head>
<body>
<div id="app">
<h2>总价格:{{totalPrices}}</h2>
</div>
<script src="../JS/vue.js"></script>
<script>
const app = new Vue({
el: "#app",
data: {
books: [
{ id: 110, name: "unix编程", price: 119 },
{ id: 111, name: "代码大全", price: 105 },
{ id: 112, name: "js教程", price: 98 },
{ id: 113, name: "vue教程", price: 87 },
],
},
computed: {
totalPrices: function () {
let result = 0;
for (let i = 0; i < this.books.length; i++) {
result += this.books[i].price;
}
return result;
},
},
// methods比较适合循环输出,调几次就几次,没有缓存
// 计算属性在多次调用时只调用一次 ,有缓存,节省性能
});
</script>
</body>
</html>
语法糖
v-bind -> :
v-on-> @