Vue.js阶段总结1

VUE基础

参考链接:https://cn.vuejs.org/v2/guide/

v-model:

表单输入和应用之间的双向绑定,可与事件监听配合,通过事件和输入值修改变量值

v-on:添加一个事件监听器,比如click点击事件 v-on:click=“box” 简写为@click=“box”

v-for:绑定数组的数据来渲染一个项目列表

v-bind:绑定属性 v-bind:name="tt"简写为 :name=“tt”

v-text:标签内的文本输出, v-text=“name” 文本输出变量name的值

v-html: 可以去标签输出变量的值 。

v-if:条件 其他 后面值自动转化为布尔值 true显示 false 隐藏内容和标签 v-if v-else 中间不可以有其他标签 template除外

v-show:控制显影 后面转化布尔值

template:就像div 包裹成块 检查不显示标签

1
prevent阻止刷新跳转的默认行为   stop阻止自身以外的冒泡,self只会阻止自身冒泡
1
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
{{name}} <h1>{{name}}</h1> // 变量
<h2 v-text="name"></h2> // 覆盖原来的值
<h2 v-text="name">YYYYY</h2>
{{'name'}} // 字符串
<span v-text="'name'"></span>
若变量带有标签,标签实体化 去标签
<h1>{{hd}}</h1> <h1 v-text="hd"></h1> <h1 v-html="hd"></h1>
<hr>
表单
<input type="text" v-model="name">
<hr>
应用三元表达式
{{hh>30?"是的":'no'}}
<h2 v-text="hh>20?'是的':'no'"></h2>
连字符+:变量 字符串
<h3 v-text="hh+'hh'"></h3>
<hr>
template就像div 包裹成块 检查不显示标签
v-if:条件 其他 后面值自动转化为布尔值 true显示 false 隐藏内容和标签
v-if v-else 中间不可以有其他标签 template除外
<p v-if="hh>10">nihaoma </p>
<p v-else>hahah</p>
<template v-if="yy">
动态属性绑定 即v-bind 后面的元素和变量绑定(检查中看)
绑定简写 即 :标签属性=vue变量 属性名随便写
属性后面不会自动转化为true和false
<h1 v-bind:name="tt">11</h1> <h1 v-bind:name="'tt'">11</h1> <h1 :name="tt">11</h1>
<input type="text" v-model="tt">
</template>
<p v-else>hahah</p>
控制显影 后面转化布尔值
<h2 v-show="0">大家好</h2>
<h4 ></h4>
</div>
<script>
一个id对应一个new Vue
new Vue({
抓id
el:'#app',
变量申明
data:{
name:'houdun',
hd:'<span>世界如此美好</span>',
hh:'11',
tt:'你好',
yy:1,
},
})
</script>
</body>
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="vue.js"></script>
<style>
.box{
color:red;
}
.box1{
color:green;
}
.box2{
color:blue;
}
</style>
</head>
<body>
<div id="qq">
<table border="1">
<tr>
<th>编号</th>
<th>种类</th>
<th>性别</th>
</tr>
<!--在标签内循环 v值,k键 in 数组-->
<tr v-for="(v,k) in menus">
<th>{{k}}</th>
<th>{{v.type}}</th>
<th>{{v.sex}}</th>
</tr>
</table >
<input type="text" v-model="num">
<!--<button v-on:click="box">敢死队多少人</button>-->
<!--click事件添加一个事件监听器,v-on:简写@,事件简写-->
<button @click="box">敢死队多少人</button>

<hr>
<p v-if="goods.legth==0">
没东西诶~
</p>
<template v-else>
<table border="1">
<tr>
<td>编号</td>
<td>商品名称</td>
<td>价格</td>
<td>数量</td>
<td>小计</td>
<th>操作</th>
</tr>
<tr v-for="(v1,k1) in goods">
<td>{{k1}}</td>
<td>{{v1.name}}</td>
<td>{{v1.price}}</td>
<td>
<button @click="reduce(v1)">-</button>
<input type="text" v-model="v1.number">
<button @click="plus(v1)">+</button>
</td>
<td>{{v1.price*v1.number}} </td>
<th>
<button @click="del(k1)">删除</button>
</th>
</tr>
</table>
<div>总计:{{totalPrice}}</div>
</template>
<hr>
<p class="box">后盾人</p>
<!-- 绑定用于属性 双引号""解析变量,单引号''不解析变量-->
<p :class="'box'">后盾</p>
<p :class="uu">后盾</p>
<!--表单数据的双向绑定 值为vue变量-->
<input type="text" v-model="uu">
<!--隔离符 里面只能放类名, 变量不解析,用不了属性;类名:true/false 是否使用这个类-->
<p :class="{hd:true,box:true,box1:false}">你是谁</p>
<p class="hd box"></p>
<!--数组中变量能解析,属性正常用 类名必须加上单引号才能用,否则失效-->
<p :class="[red,'box1']">你好世界</p>
<p :style="{color:color,fontSize:size+'px'}">hahahhah</p>
<hr>
<!--隔离符 变量名再值位置可解析 可能是html标签属性本身的规则 -->
<p style="color:red;font-size:10px">hahahhah</p>

<hr>
<!--prevent阻止刷新跳转的默认行为 stop阻止自身以外的冒泡,self只会阻止自身冒泡-->
<a href="http://www.baidu.com" @click.prevent="box">点我</a>
<a href="http://www.baidu.com" @click="box1">点我</a>

<div style="width: 200px;height: 200px;background: red" @click="re">
<a href="" style="display: block;width: 100px;
height: 100px;background: blue" @click.stop.prevent="blue"></a>
</div>
<hr>
<div style="width: 200px;height: 200px;background: red" @click.self="re">
<a href="" style="display: block;width: 100px;height: 100px;
background: blue" @click.prevent="blue"></a>
</div>
</div>
<script>
new Vue({
el:'#qq',
// 数据 属性
data:{
menus:[
{id:1,type:'zhu',sex:'公'},
{id:2,type:'zhu',sex:'母'},
{id:3,type:'zhu',sex:'母'},
],
num:1,
number:1,
uu:'box',
red:'box',
color:'red',
hd:'box',
size:20,
goods:[
{name:'充电宝',price:120,number:1},
{name:'山寨电风扇',price:200,number:1},
{name:'山寨手机',price:1000,number:1},
{name:'山寨老师',price:10,number:1},
]
},
// 事件
methods:{
//方法中句用 ; 分号隔开
box:function (){
// 本类数据中的num属性
this.num++
},
reduce(v1){
if(v1.number<1){
return false;
}
v1.number--;
},
plus(v1){
// 参数参与
v1.number++;
},
del(k1){
//从数组中添加/删除项目,然后返回被删除的项目。 位置,数量
this.goods.splice(k1,1);
},
blue(){
alert('blue')
},
re(){
alert('red');
},
box1() {
alert(2);
//阻止a默认跳转
event.preventDefault();
},
},
//计算 属性?
computed:{
totalPrice(){
var total=0;
//箭头函数 调用本类中的数据goods
this.goods.forEach((v1,k1)=>{
total+=v1.number*v1.price;
})
return total;
}
}
})
</script>
</body>
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<style>
/* 目标路由成功激活时,自动使用*/
.router-link-active{
color:red;
}
</style>
</head>
<body>
<div id="app">
<!-- 原来的v-link指令已经被<router-link>组件替代了-->
<router-link to="/hdphp">hdphp</router-link>
<router-link to="/hdcms">hdcms</router-link>
<hr>
<!-- 用来渲染通过路由映射过来的组件-->
<router-view></router-view>
</div>
<script>
// 组件模板
const hdcms={
template:'<div>我是hdcms组件</div>',
};
const hdphp={
template: '<div>我是hdphp组件</div>'
};
const router=new VueRouter({
//配置路由和对应组件
routes:[
{path:'/hdcms',component:hdcms},
{path:'/hdphp',component:hdphp},
],
})
new Vue({
el:'#app',
router,
})
</script>
</body>
// 原来的v-link指令已经被<router-link>组件替代了,通过to属性指定目标地址,默认渲染为带有正确连接的<a>标签,
// 当目标路由成功激活时,链接元素自动设置一个表示激活的css类名
4全局、局部组件、调用父组件数据
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
有点乱,可以参考链接https://blog.csdn.net/jingtian678/article/details/81160995
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<!-- 全局组件调用-->
<h-card></h-card>
<!-- is属性调用全局组件-->
<p is="h-card"></p>
<!-- <hr>以下为局部组件调用,作为d-der标签作为父组件提供数据age-->
<d-der age="120" ></d-der>
</div>
<hr>
<!-- 尝试全局组件的调用-->
<div id="appi">
<p is="h-card"></p>
</div>

<script type="text/x-template" id="dYer">
<!-- 局部组件模板,不显示在页面上-->
<div>
我是头部哈哈
{{message}}
{{age}}
<!-- age为父组件标签dder的属性数据,用在模板中-->
</div>
</script>
<script>
//创建全局组件,指script标签包裹范围内, 组件名称驼峰命名 template属性中 要有根标签div包裹模板内容
Vue.component('hCard',{
template:'<div><h1>你好世界</h1><p>hellow world</p></div>'
})
new Vue({
el:'#app',
// 创建一个组件(局部)
components:{
dDer:{//组件名
// template:'<div> 我是谁,我叫什么,我在哪</div>',//局部组件模板
template:'#dYer',//调用id=hdHeader作为局部组件模板
// props:['age'],//放data之前 props是子组件访问父组件数据的唯一接口,用于接收
props:{
age:{//申明变量age,接收父组件数据,即dDer标签的属性数据
type:String
}
},
// data:function ()
data(){//内部数据 简写
return{
message:'是你呀'
}
}
}
}
}),
new Vue({//用于尝试调用全局组件
el:'#appi',
})
</script>
</body>
5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
{{pid}}
<hr>
<!-- 调用局部组件 添加事件名ad-->
<h-body @add="ad"></h-body>
</div>
<!-- 局部组件模板-->
<script type="text/x-template" id="hhBody">
<div>
<button @click="plus">点点加加{{num}}</button>
</div>
</script>
<script>
new Vue({
el:'#app',
data:{
pid:2,
},
components: {//局部组件
hBody:{//局部组件名
template:'#hhBody',//调用模板
data(){
return{
num:1
}
},
methods:{
plus(){
this.num++;
this.$emit('add',2);
//子组件可以使用 $emit 触发父组件自定义事件add,参数为2。
}
}
}
},
methods:{
ad(ar){
// console.log(ar);
this.pid+=ar
}
}
})
</script>
</body>