1.Vue入门
1.1 什么是Vue
Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue 也完全能够为复杂的单页应用提供驱动。
渐进式: 构建项目可以由简单到复杂
1.2 Vue优点
1).体积小 压缩后的文件只有33k
2).运行效率更高 采用虚拟机DOM,一种可以预先通过javaScript对数据进行计算.把最终的DOM操作计算出来并且优化的技术. 由于这个DOM操作属于预处理操作,并没有真实的操作DOM ,所以叫做虚拟DOM
3).双向数据绑定 让开发者不再去操作DOM,将更多的经历投入到业务中
4).生态丰富 市面上有大量的开源项目基于vue 进行开发 成熟稳定.
1.3 Vue案例
1.3.1入门案例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hello 入门案例</title> </head> <body> <div id="app"> <h1>双向数据绑定测试</h1> <h3>{{ hello }}</h3> </div>
<span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 引入js <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span><span class="token operator">></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//element 元素</span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> hello<span class="token operator">:</span> <span class="token string">"helloVue"</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
1.3.2 v-cloak属性
介绍:
这个指令保持在元素上直到关联实例结束编译。和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Mustache 标签直到实例准备完毕。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>v-clock属性</title>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>style</span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css"> <span class="token comment">/* 定义属性样式 */</span> <span class="token selector">[v-cloak]</span><span class="token punctuation">{<!-- --></span> <span class="token comment">/*将元素进行隐藏 */</span> <span class="token property">display</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span> <span class="token punctuation">}</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>style</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> 这个指令保持在元素上直到关联实例结束编译。编译成功之后属性消失 <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>app<span class="token punctuation">"</span></span> <span class="token attr-name">v-cloak</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span>v-clock属性<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h3</span><span class="token punctuation">></span></span>{<!-- -->{ hello }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h3</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 引入js --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>../js/vue.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//element 元素</span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> hello<span class="token operator">:</span> <span class="token string">"helloVue"</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
</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
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
1.3.3 数据绑定指令
1.3.3.1 v-text 指令
作用: 直接展现解析数据
<!-- v-text指令 没有闪动效果 -->
<h3 v-text="text"></h3>
<!-- 引入js -->
<script src="../js/vue.js"></script>
<script type="text/javascript">
const app = new Vue({
//element 元素
el: "#app",
data: {
hello: "helloVue",
text: "数据绑定text"
}
})
</script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
1.3.3.2 v-html 指令
说明: 解析html的数据
1.3.3.3 v-pre 指令
用法:
跳过这个元素和它的子元素的编译过程。可以用来显示原始 Mustache 标签。跳过大量没有指令的节点会加快编译。
编辑html:
1.3.3.4 v-once 指令
用法:
只渲染元素和组件一次。随后的重新渲染,元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。
html用法:
控制台测试
app.once 则页面渲染内容不变.
1.3.3.5 v-model 指令
用法: 在表单控件或者组件上创建双向绑定,当js数据修改时,页面内容变化, 当页面内容修改时,则数据变化.
1.3.4 MVVM思想
1).字母解释
1.M model 数据
2.V view 视图
3.VM (view-model) 数据和视图的控制
2).当页面数据发生变化时,则通过dom监听将数据传给model
当model的数据发生变化时,则通过数据绑定 绑定到页面中
1.4 事件绑定
1.4.1 v-on指令
1). <button v-on:click="num++">增加1</button> 绑定点击事件
2).<button @click="num++">增加1</button> 简化写法
3).<button @click="addNum">增加1</button>
- 1
- 2
- 3
4).
<button @click=“addNum2($event)”>传递event对象
1.4.2 事件修饰符
1). 阻止冒泡 .stop
2). 阻止默认行为 @click.prevent
1.4.3 按键修饰符
<div>
<input name="username" type="text" @keyup.enter="handler" value="回车触发"/>
<input name="username" type="text" @keyup.space="handler" value="空格触发"/>
<input name="username" type="text" @keyup.delete="handler" value="删除键触发"/>
<input name="username" type="text" @keyup.left="handler" value="<-触发"/>
<input name="username" type="text" @keyup.right="handler" value="->键触发"/>
</div>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
1.4.4 综合案例练习
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>计算器案例</title> </head> <body>
<span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"app"</span><span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>h1<span class="token punctuation">></span></span>实现计算器功能<span class="token operator"><</span><span class="token operator">/</span>h1<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>div<span class="token punctuation">></span></span> <span class="token generics"><span class="token punctuation"><</span>div<span class="token punctuation">></span></span>数据<span class="token class-name">A</span><span class="token operator">:</span><span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> name<span class="token operator">=</span><span class="token string">"num1"</span> v<span class="token operator">-</span>model<span class="token operator">=</span><span class="token string">"num1"</span><span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>div<span class="token punctuation">></span></span>数据<span class="token class-name">B</span><span class="token operator">:</span><span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> name<span class="token operator">=</span><span class="token string">"num2"</span> v<span class="token operator">-</span>model<span class="token operator">=</span><span class="token string">"num2"</span><span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>button type<span class="token operator">=</span><span class="token string">"button"</span> <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"count"</span><span class="token operator">></span>计算<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>div<span class="token punctuation">></span></span>结果<span class="token operator">:</span><span class="token operator"><</span>span v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"result"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span> src<span class="token operator">=</span><span class="token string">"../../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span><span class="token operator">></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data <span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> num1 <span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> num2 <span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> result <span class="token operator">:</span> <span class="token string">''</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> methods<span class="token operator">:</span><span class="token punctuation">{<!-- --></span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>result <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>num1<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>num2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//this.result = eval(this.num1) + eval(this.num2)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
1.5 属性绑定
1.5.1 v-bind属性绑定
1.5.2 v-bind Class样式绑定
<div> 样式绑定 <div v-bind:class="{red: isActive}">样式测试</div> <button @click="isActive = !isActive">切换</button> </div>
<span class="token generics"><span class="token punctuation"><</span>div<span class="token punctuation">></span></span> 样式数组写法 <span class="token operator"><</span>div v<span class="token operator">-</span>bind<span class="token operator">:</span><span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"[redClass,fontSize]"</span><span class="token operator">></span>属性样式测试<span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"changeCss"</span><span class="token operator">></span>切换<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> url<span class="token operator">:</span> <span class="token string">"http://www.baidu.com"</span><span class="token punctuation">,</span> url2<span class="token operator">:</span> <span class="token string">"http://www.jd.com"</span><span class="token punctuation">,</span> isActive<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> redClass<span class="token operator">:</span> <span class="token string">"red"</span><span class="token punctuation">,</span> fontSize<span class="token operator">:</span> <span class="token string">"fontSize"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> methods<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token function">changeCss</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>redClass <span class="token operator">=</span> <span class="token string">''</span> <span class="token keyword">this</span><span class="token punctuation">.</span>fontSize <span class="token operator">=</span> <span class="token string">''</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span>
- 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
1.5.3 v-bind Style样式绑定
1).样式绑定
style 样式绑定 <div v-bind:style="{border: borderStyle,width: widthStyle,height: heightStyle}">123</div>
data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> url<span class="token operator">:</span> <span class="token string">"http://www.baidu.com"</span><span class="token punctuation">,</span> url2<span class="token operator">:</span> <span class="token string">"http://www.jd.com"</span><span class="token punctuation">,</span> isActive<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> redClass<span class="token operator">:</span> <span class="token string">"red"</span><span class="token punctuation">,</span> fontSize<span class="token operator">:</span> <span class="token string">"fontSize"</span><span class="token punctuation">,</span> borderStyle<span class="token operator">:</span> <span class="token string">"1px solid blue"</span><span class="token punctuation">,</span> widthStyle<span class="token operator">:</span> <span class="token string">"100px"</span><span class="token punctuation">,</span> heightStyle<span class="token operator">:</span> <span class="token string">"100px"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2).对象封装写法
<div v-bind:style="myStyle">456</div>
data: {
myStyle : {
border: '2px solid green',
width: "20px",
height: "20px"
}
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
1.6 分支结构语法
1.6.1 分支结构说明
1).v-if
2).v-else
3).v-else-if
4).v-show
1.6.2 分支结构用法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>分支结构语法</title> </head> <body> <div id="app" align="center"> <h1>根据根据分数评级</h1> <!-- v-if当判断条件成立时 div展现 控制dom元素增加 开销较大 --> <div v-if="score>=90">优秀</div> <div v-else-if="score>=80">良好</div> <div v-else-if="score>=70">中等</div> <div v-else>不及格</div>
<span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 直接渲染到页面中采用display<span class="token operator">:</span> none<span class="token punctuation">;</span>隐藏属性 如果频繁展现的元素 使用v<span class="token operator">-</span>show <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>div v<span class="token operator">-</span>show<span class="token operator">=</span><span class="token string">"flag"</span><span class="token operator">></span>测试show数据<span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"flag= !flag"</span><span class="token operator">></span>展现<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//定义分数</span> score<span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span> flag<span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 28
- 29
- 30
- 31
- 32
- 33
- 34
1.7 循环结构
1.7.1 v-for循环
<div id="app"> <!-- 插值表达式如果渲染不完全会展现{{}} --> <span v-for="item in hobby">{{item}}</span> <!-- 使用v-text优化属性 --> <span v-for="item in hobby" v-text="item"></span> <!-- 展现数组下标 --> <span v-for="(item,index) in hobby" v-text="index"></span>
<span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 循环遍历数组对象 <span class="token operator">:</span>key用来区分遍历的节点信息 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>div v<span class="token operator">-</span><span class="token keyword">for</span><span class="token operator">=</span><span class="token string">"item in users"</span> <span class="token operator">:</span>key<span class="token operator">=</span><span class="token string">"item.id"</span><span class="token operator">></span> <span class="token operator"><</span>span v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"item.id"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span> <span class="token operator"><</span>span v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"item.name"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 遍历对象 获取对象的key<span class="token operator">-</span>value<span class="token operator">-</span>index索引 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>div v<span class="token operator">-</span><span class="token keyword">for</span><span class="token operator">=</span><span class="token string">"(value,key,index) in user"</span><span class="token operator">></span> <span class="token operator"><</span>span v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"key"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span> <span class="token operator"><</span>span v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"value"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span> <span class="token operator"><</span>span v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"index"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data <span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> hobby<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'吃'</span><span class="token punctuation">,</span><span class="token string">'喝'</span><span class="token punctuation">,</span><span class="token string">'玩'</span><span class="token punctuation">,</span><span class="token string">'乐'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> users<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span> id<span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'王昭君'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span> id<span class="token operator">:</span> <span class="token number">101</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'安琪拉'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span> user<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> id<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'张三'</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token number">18</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span>
- 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
1.8 Vue常用特性
1.8.1 表单操作
1.8.1.1 表单常用属性
- input 文本输入框
- textarea 多行文本
- select 下拉多选框
- radio 单选框
- checkbok 多选框
1.8.1.2 表单数据与vue进行数据绑定的写法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>常用表单属性</title> </head> <body> <h1>本案例练习 表单提交数据时 数据如何与vue进行数据绑定</h1> <div id="app"> <form id="userForm"action="http://www.baidu.com"> <div> <span> 姓名: </span> <span> <input type="text" name="name" v-model="name"/> </span> </div> <div> <span>性别:</span> <span> <input type="radio" name="gender" value="男" id="man" v-model="gender"/> <label for="man">男</label> <input type="radio" name="gender" value="女" id="women" v-model="gender"/> <label for="women">女</label> </span> </div> <div> <span>爱好:</span> <input type="checkbox" name="hobbies" value="吃" v-model="hobbies"/>吃 <input type="checkbox" name="hobbies" value="喝" v-model="hobbies"/>喝 <input type="checkbox" name="hobbies" value="玩" v-model="hobbies"/>玩 </div> <div> <span>职业</span> <!-- 如果需要设置为多选 则添加属性 --> <select name="occupation" v-model="occupation" multiple="true"> <option value="工人">工人</option> <option value="教师">教师</option> <option value="工程师">工程师</option> </select> </div> <div> <span>个人简介</span> <textarea name="userInfo" style="width: 200px;height: 50px;" v-model="userInfo"></textarea> </div> <div> <!-- 阻止默认提交事件 --> <input type="submit" value="提交" v-on:click.prevent="submitForm"/> </div> </form> </div>
<span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 引入JS文件 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> name<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> gender<span class="token operator">:</span> <span class="token string">'男'</span><span class="token punctuation">,</span> <span class="token comment">//多个数据定义时 应该使用数组</span> hobbies<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'吃'</span><span class="token punctuation">,</span><span class="token string">'喝'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> occupation<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'工人'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> userInfo<span class="token operator">:</span> <span class="token string">''</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> methods<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token function">submitForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//数据提交</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"姓名:"</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"性别:"</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>gender<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'爱好:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>hobbies<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'职业:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>occupation<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'用户详情:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>userInfo<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'封装好数据之后,可以使用ajax方式实现数据提交'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
1.8.2 表单域修饰符
1.8.2.1 常用属性
1).number : 将数据转化为数值
2).trim: 去掉开始和结尾的空格
3).lazy: 将input事件 切换为change事件
1.8.2.2 number属性
1.8.2.2 trim属性
1.8.2.3 lazy属性
1.8.3 计算属性
1.8.3.1 为什么需要计算属性
由于业务需要,表达式的业务逻辑可能比较复杂 阅读起来不够简洁,通过计算属性可以优化代码结构
1.8.3.2 计算属性案例
1.8.4 计算属性与方法的区别
1).计算属性有缓存 方法没有缓存
1.8.5 侦听器
1.8.5.1 监听器作用
当属性数据发生变化 则通知监听器所绑定的方法 一般多用于执行异步操作
1.8.5.2 配置监听器
1.8.5.3 监听器案例实现
1.8.6 过滤器
1.8.6.1 过滤器作用
格式化数据,比如格式化日期,特殊数据格式时使用
1.8.6.2 过滤器用法
1.9 VUE生命周期
1.9.1 主要阶段
1.9.2 页面结构调用
只要熟练掌握 mounted 就可以了
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>测试vue生命周期函数</title> </head> <body>
<span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"app"</span><span class="token operator">></span> <span class="token operator"><</span>h3 v<span class="token operator">-</span>text<span class="token operator">=</span><span class="token string">"msg"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>h3<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"destroy"</span><span class="token operator">></span>销毁<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入js函数类库 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data <span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> msg<span class="token operator">:</span> <span class="token string">"vue生命周期"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用。</span> <span class="token function">beforeCreate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"beforeCreate"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//在实例创建完成后被立即调用</span> <span class="token function">created</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"created"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//在挂载开始之前被调用:相关的 render 函数首次被调用。</span> <span class="token function">beforeMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"beforeMount"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//实例被挂载后调用,这时 el 被新创建的 vm.$el 替换了。</span> <span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"mounted"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//数据更新时调用,发生在虚拟 DOM 打补丁之前</span> <span class="token function">beforeUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"beforeUpdate"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。</span> <span class="token function">updated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"updated"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//实例销毁之前调用。在这一步,实例仍然完全可用</span> <span class="token function">beforeDestroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"beforeDestroy"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//实例销毁后调用。</span> <span class="token function">destroyed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"destroyed"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> methods<span class="token operator">:</span><span class="token punctuation">{<!-- --></span> <span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>$<span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
1.10 VUE数组操作
1.10.1 文档位置
1.10.2 数组用法介绍
1.push() 在最后一个追加
2.pop() 删除最后一个
3.shift() 删除第一个元素
4.unshift() 在开头追加一个元素
5.splice() 在指定位置替换元素
6.sort() 数组排序 默认是按照字符编码的顺序进行排序
7.reverse() 数组反转
1.10.3 数组使用案例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>数组用法</title> </head> <body>
<span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"app"</span><span class="token operator">></span> <span class="token operator"><</span>span v<span class="token operator">-</span><span class="token keyword">for</span><span class="token operator">=</span><span class="token string">"num in array"</span><span class="token operator">></span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>num<span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator"><</span><span class="token operator">/</span>span<span class="token operator">></span> <span class="token operator"><</span>hr <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> name<span class="token operator">=</span><span class="token string">"num"</span> v<span class="token operator">-</span>model<span class="token operator">=</span><span class="token string">"num"</span><span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"add"</span><span class="token operator">></span>添加<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"del"</span><span class="token operator">></span>删除<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"shift"</span><span class="token operator">></span>删除第一个<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"upshift"</span><span class="token operator">></span>从第一个元素添加<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"splice"</span><span class="token operator">></span>替换数据<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"sort"</span><span class="token operator">></span>排序<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>button <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"reverse"</span><span class="token operator">></span>反转<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入js函数类库 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data <span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> array<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> num<span class="token operator">:</span> <span class="token string">''</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> methods<span class="token operator">:</span><span class="token punctuation">{<!-- --></span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>num<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">del</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">upshift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>num<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">splice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//参数说明: 1.操作数据起始位置 2. 操作数量 3.替换元素的值(如果不写表示删除)</span> <span class="token comment">//将第二位进行替换</span> <span class="token comment">//this.array.splice(1,1,this.num)</span> <span class="token comment">//删除第二位数据</span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//数组从小到大</span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//数组反转</span> <span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
2.VUE组件化
2.1 组件介绍
组件系统是 Vue 的另一个重要概念,因为它是一种抽象,允许我们使用小型、独立和通常可复用的组件构建大型应用。仔细想想,几乎任意类型的应用界面都可以抽象为一个组件树.
使用组件可以将一些重复的内容进行封装.各个组件单独维护.体现了分治的思想(分布式思想)
补充知识: 为了保证组件化 相互之间互不干扰,则应该在组件内部 单独定义html/js/css.
2.2 组件注册
2.2.1 基本语法
注意事项:
1.组件中的data 是一个函数 data(){return {}}
2.组件模版的内容必须是一个根元素.
3.组件如果采用驼峰规则命名,则使用-线进行关联
2.2.2 案例使用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>组件注册</title> </head> <body>
<span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"app"</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 驼峰规则使用<span class="token operator">-</span>线隔开 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>add<span class="token operator">-</span>num<span class="token operator">-</span>component<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>add<span class="token operator">-</span>num<span class="token operator">-</span>component<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 组件可以复用 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>add<span class="token operator">-</span>num<span class="token operator">-</span>component<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>add<span class="token operator">-</span>num<span class="token operator">-</span>component<span class="token operator">></span> <span class="token operator"><</span>add<span class="token operator">-</span>num<span class="token operator">-</span>component<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>add<span class="token operator">-</span>num<span class="token operator">-</span>component<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 定义模版标签 必须使用跟标签定义不然编译无效 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"addNumTem"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> 数值<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>num<span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator"><</span>button @click<span class="token operator">=</span><span class="token string">"addNum"</span><span class="token operator">></span>添加<span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入js函数类库 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script<span class="token operator">></span> <span class="token comment">//1.定义组件 Vue.component('组件名称',组件实体内容)</span> Vue<span class="token punctuation">.</span><span class="token function">component</span><span class="token punctuation">(</span><span class="token string">'addNumComponent'</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span> num<span class="token operator">:</span> <span class="token number">0</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//与template标签进行关联 注意使用选择器</span> template<span class="token operator">:</span> <span class="token string">"#addNumTem"</span><span class="token punctuation">,</span> methods<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token function">addNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span>num <span class="token operator">++</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data <span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
2.3 局部组件注册
2.3.1 通过components定义局部组件
2.3.2 编辑组件demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>组件注册</title> </head> <body>
<span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"app"</span><span class="token operator">></span> <span class="token operator"><</span>hello1<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>hello1<span class="token operator">></span> <span class="token operator"><</span>hello2<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>hello2<span class="token operator">></span> <span class="token operator"><</span>hello3<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>hello3<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 定义模版代码<span class="token number">1</span> <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"hello1"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>msg<span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"hello2"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>msg<span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"hello3"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>name<span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 在全局组件中引用局部组件 使用不生效 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token operator"><</span>hello1<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>hello1<span class="token operator">></span> <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入js函数类库 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script<span class="token operator">></span> <span class="token comment">//定义全局组件</span> Vue<span class="token punctuation">.</span><span class="token function">component</span><span class="token punctuation">(</span><span class="token string">'hello3'</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span> name<span class="token operator">:</span> <span class="token string">'定义全局组件'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> template<span class="token operator">:</span> <span class="token string">"#hello3"</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">/* 定义局部组件 */</span> <span class="token keyword">let</span> hello1 <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span> msg<span class="token operator">:</span> <span class="token string">'你好Hello1'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> template<span class="token operator">:</span> <span class="token string">"#hello1"</span> <span class="token punctuation">}</span> <span class="token keyword">let</span> hello2 <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span> msg<span class="token operator">:</span> <span class="token string">'你好Hello2'</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> template<span class="token operator">:</span> <span class="token string">"#hello2"</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el <span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data <span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//只能在当前元素中使用</span> components<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token string">'hello1'</span> <span class="token operator">:</span> hello1<span class="token punctuation">,</span> <span class="token string">'hello2'</span> <span class="token operator">:</span> hello2 <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
3.Vue前端交互
3.1 Promise概述
Promise是一种异步编程的一种解决方案.从语法上将Promise是一个对象.从它身上可以获取异步调用的信息
作用:
1.有效避免回调地狱问题 典型的Ajax嵌套问题
2.Promise对象提供了简洁的API 用法简单
3.2 Promise基本用法
3.3 Promise API介绍
1.then 异步调用正确时返回
2.catch 获取异常信息
3.finally 成功与否都会执行的操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>promise调用API</title> </head> <body> <div id="app"> </div> <script>
<span class="token keyword">let</span> promise <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span>reject</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">let</span> flag <span class="token operator">=</span> <span class="token boolean">true</span> <span class="token keyword">if</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">"业务执行成功"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span> <span class="token function">reject</span><span class="token punctuation">(</span><span class="token string">"业务执行失败"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//通过then获取回调函数</span> promise<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//从resolve中获取数据</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">finally</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"最终都会执行"</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
3.4 Axios方式
3.4.1 Axios介绍
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
特点:
1.从浏览器中创建 XMLHttpRequests
2.从 node.js 创建 http 请求
3.支持 Promise API
4.拦截请求和响应
5.转换请求数据和响应数据
6.取消请求
7.自动转换 JSON 数据
8.客户端支持防御 XSRF
3.4.2 Axios入门案例
关于result的说明:
3.4.3 Axios GET/DELETE调用
1).get方式
2).带参数的方式
3.restFul风格方式
4.params参数方式
3.4.4 Axios post/put 调用
3.4.4.1 对象方式提交数据
1).编辑页面JS
2).编辑后台Controller
3.4.4.2 restFul方式提交数据
1).编辑页面JS
2).编辑后台Controller
3.4.4.3 form表单数据
1).编辑页面JS
2).页面提交参数结构
3.编辑Controller
3.4.5 Axios 配置信息
//配置基本请求路径
axios.defaults.baseURL = "http://localhost:8080/"
//设定请求头信息
axios.defaults.headers['mytoken'] = 'token-123456'
- 1
- 2
- 3
- 4
3.4.6 Axios 拦截器机制
3.4.6.1 请求拦截器
//1.定义请求拦截器
axios.interceptors.request.use(function(config){
//console.log(config)
//在请求发出前进行信息设置
config.headers.mytoken = 'qwerqwerqwer'
return config
},function(error){
//报错信息返回
console.log(error)
})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.4.6.2 响应拦截器
//2.定义响应拦截器 axios.interceptors.response.use(function(response){ console.log(response) return response },function(error){ console.log(error) })
axios<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"http://localhost:8080/getJSON"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//console.log(result.data)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3.4.7 async-await用法
3.4.7.1 介绍
1.async/await 是ES7引入的新语法 可以更加方便的进行异步操作
2.async 关键字用在函数上. 返回值是一个promise对象
3.await 关键字用在async 函数中
3.4.7.2 入门案例用法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Axios配置</title> </head> <body>
<span class="token operator"><</span>h1<span class="token operator">></span>远程Ajax测试<span class="token operator"><</span><span class="token operator">/</span>h1<span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/axios.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script<span class="token operator">></span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">getJSON</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//返回一个promise对象</span> <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token keyword">await</span> axios<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'http://localhost:8080/getJSON'</span><span class="token punctuation">)</span> <span class="token comment">//通过data获取数据</span> <span class="token keyword">return</span> result<span class="token punctuation">.</span>data <span class="token punctuation">}</span> <span class="token comment">//调用函数</span> <span class="token function">getJSON</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
3.4.8 ajax教学案例
3.4.8.1 编辑页面html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>用户列表展现案例</title> </head> <body> <div id="app"> <h1 align="center">用户列表展现案例</h1> <table align="center" border="1px" width="800px"> <tr align="center"> <td>ID编号</td> <td>姓名</td> <td>年龄</td> <td>性别</td> <td>操作</td> </tr> <tr align="center" v-for="user in userList" :key="user.id"> <td>{{user.id}}</td> <td>{{user.name}}</td> <td>{{user.age}}</td> <td>{{user.sex}}</td> <td> <button @click="updateUser(user)">修改</button> <button @click="deleteUser(user)">删除</button> </td> </tr> </table> <hr /> <div> <h3 align="center">用户修改操作</h3><br> <p> 用户ID号: <input type="text" name="id" v-model="user.id" disabled/> 用户名称: <input type="text" name="name" v-model="user.name"/> </p> <p> 用户年龄: <input type="text" name="age" v-model="user.age"/> 用户性别: <input type="text" name="sex" v-model="user.sex"/> </p> <p> <button @click="updateUserBtn">修改</button> </p> </div> </div>
<span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/axios.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token generics"><span class="token punctuation"><</span>script<span class="token punctuation">></span></span> <span class="token comment">//1.定义默认的axios请求路径</span> axios<span class="token punctuation">.</span>defaults<span class="token punctuation">.</span>baseURL <span class="token operator">=</span> <span class="token string">"http://localhost:8090/"</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> data<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//准备用户数据</span> userList<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> user<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> id<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> sex<span class="token operator">:</span> <span class="token string">''</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> methods<span class="token operator">:</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//1.发起get请求</span> async <span class="token function">getUserList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span>data<span class="token operator">:</span> result<span class="token punctuation">}</span> <span class="token operator">=</span> await axios<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/getUserList'</span><span class="token punctuation">)</span> <span class="token keyword">this</span><span class="token punctuation">.</span>userList <span class="token operator">=</span> result <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">updateUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//1.获取页面修改的数据</span> <span class="token keyword">this</span><span class="token punctuation">.</span>user <span class="token operator">=</span> user <span class="token punctuation">}</span><span class="token punctuation">,</span> async <span class="token function">updateUserBtn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> await axios<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">'updateUser'</span><span class="token punctuation">,</span><span class="token keyword">this</span><span class="token punctuation">.</span>user<span class="token punctuation">)</span> <span class="token comment">//重新加载页面数据</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUserList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> async <span class="token function">deleteUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> let id <span class="token operator">=</span> user<span class="token punctuation">.</span>id await axios<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">'user/'</span><span class="token operator">+</span>id<span class="token punctuation">)</span> <span class="token comment">//删除之后,重新加载数据</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUserList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUserList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
3.4.8.2 编辑页面Controller
package com.jt.controller;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
-
@author 刘昱江
-
时间 2021/5/9
*/
@RestController
@CrossOrigin
public class UserController {@Autowired
private UserService userService;@GetMapping("/getUserList")
public List<User> findAll(){<span class="token keyword">return</span> userService<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
}
@PutMapping("/updateUser")
public void updateUser(@RequestBody User user){userService<span class="token punctuation">.</span><span class="token function">updateUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
}
@DeleteMapping("/user/{id}")
public void deleteUser(@PathVariable Integer id){
userService.deleteUser(id);
}
}
- 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
4 VUE路由
4.1 VUE Router介绍
Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:
嵌套的路由/视图表
模块化的、基于组件的路由配置
路由参数、查询、通配符
基于 Vue.js 过渡系统的视图过渡效果
细粒度的导航控制
带有自动激活的 CSS class 的链接
HTML5 历史模式或 hash 模式,在 IE9 中自动降级
自定义的滚动条行为
4.2 Router 路由入门案例
4.2.1 Router使用步骤
- 引入相关的库文件
- 添加路由链接
- 添加路由填充位
- 定义路由组件
- 配置路由规则并且创建路由实例
- 把路由挂载到Vue根实例中
4.2.2 Router 入门案例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>路由实例</title> </head> <body> <div id="app">
<span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token number">2.</span>添加路由连接 Vue中默认添加路由规则 <span class="token number">1.</span>router<span class="token operator">-</span>link会默认渲染为a标签 <span class="token number">2.</span>to属性默认渲染为 href标签 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>router<span class="token operator">-</span>link to<span class="token operator">=</span><span class="token string">"/user"</span><span class="token operator">></span>用户<span class="token operator"><</span><span class="token operator">/</span>router<span class="token operator">-</span>link<span class="token operator">></span> <span class="token operator"><</span>router<span class="token operator">-</span>link to<span class="token operator">=</span><span class="token string">"/dog"</span><span class="token operator">></span>狗<span class="token operator"><</span><span class="token operator">/</span>router<span class="token operator">-</span>link<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token number">3.</span>定义路由填充位 将来通过路由匹配到的组件<span class="token punctuation">,</span>将通过路由填充位进行展现 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>router<span class="token operator">-</span>view<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>router<span class="token operator">-</span>view<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入js函数类库 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 引入路由规则 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue-router.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script<span class="token operator">></span> <span class="token comment">//4.定义user组件</span> <span class="token keyword">const</span> user <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> template<span class="token operator">:</span> <span class="token string">'<h1>我是User组件</h1>'</span> <span class="token punctuation">}</span> <span class="token comment">//定义dog组件</span> <span class="token keyword">const</span> dog <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> template<span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string"><h1>我是Dog组件</h1></span><span class="token template-punctuation string">`</span></span> <span class="token punctuation">}</span> <span class="token comment">//5.创建路由规则</span> <span class="token comment">//属性routes[] 定义路由规则</span> <span class="token comment">//path: 定义路径</span> <span class="token comment">//component: 定义组件名称</span> <span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//routes 定义路由规则</span> routes<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{<!-- --></span>path<span class="token operator">:</span><span class="token string">"/user"</span><span class="token punctuation">,</span>component<span class="token operator">:</span> user<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{<!-- --></span>path<span class="token operator">:</span><span class="token string">"/dog"</span><span class="token punctuation">,</span>component<span class="token operator">:</span> dog<span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//6.将路由挂载到Vue实例中</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el<span class="token operator">:</span> <span class="token string">"#app"</span><span class="token punctuation">,</span> <span class="token comment">//挂载路由实例对象 如果名称一致 可以简化</span> <span class="token comment">//router: router</span> router <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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
4.3 Router 重定向
说明: 当用户访问A地址时,强制跳转访问B 则需要使用路由重定向技术
关键子: redirect
用法如下:
4.3 Router 嵌套
核心代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>路由实例</title> </head> <body> <div id="app"> <router-link to="/shopping">商场</router-link> <router-link to="/zoo">动物园</router-link> <router-view></router-view> </div>
<span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"shopping"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token operator"><</span>h3<span class="token operator">></span>商场组件<span class="token operator"><</span><span class="token operator">/</span>h3<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 定义子路由 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>router<span class="token operator">-</span>link to<span class="token operator">=</span><span class="token string">"/shopping/shoe"</span><span class="token operator">></span>鞋店<span class="token operator"><</span><span class="token operator">/</span>router<span class="token operator">-</span>link<span class="token operator">></span> <span class="token operator"><</span>router<span class="token operator">-</span>link to<span class="token operator">=</span><span class="token string">"/shopping/huawei"</span><span class="token operator">></span>华为手机店<span class="token operator"><</span><span class="token operator">/</span>router<span class="token operator">-</span>link<span class="token operator">></span> <span class="token operator"><</span>router<span class="token operator">-</span>view<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>router<span class="token operator">-</span>view<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"zoo"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token operator"><</span>h3<span class="token operator">></span>动物园组件<span class="token operator"><</span><span class="token operator">/</span>h3<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"shoe"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token operator"><</span>h3<span class="token operator">></span>鞋的组件<span class="token operator"><</span><span class="token operator">/</span>h3<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span>template id<span class="token operator">=</span><span class="token string">"huawei"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span> <span class="token operator"><</span>h3<span class="token operator">></span>欢迎光临华为手机店<span class="token operator"><</span><span class="token operator">/</span>h3<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入js函数类库 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 引入路由规则 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"../js/vue-router.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>script<span class="token operator">></span> <span class="token comment">//1.定义商场组件</span> <span class="token keyword">const</span> shopping <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> template<span class="token operator">:</span> <span class="token string">"#shopping"</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> zoo <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> template<span class="token operator">:</span> <span class="token string">"#zoo"</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> shoe <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> template<span class="token operator">:</span> <span class="token string">"#shoe"</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> huawei <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span> template<span class="token operator">:</span> <span class="token string">"#huawei"</span> <span class="token punctuation">}</span> <span class="token comment">//定义路由对象</span> <span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> routes<span class="token operator">:</span><span class="token punctuation">[</span> <span class="token punctuation">{<!-- --></span>path<span class="token operator">:</span><span class="token string">'/shopping'</span><span class="token punctuation">,</span> component<span class="token operator">:</span>shopping<span class="token punctuation">,</span> children<span class="token operator">:</span><span class="token punctuation">[</span> <span class="token punctuation">{<!-- --></span>path<span class="token operator">:</span><span class="token string">'/shopping/shoe'</span><span class="token punctuation">,</span> component<span class="token operator">:</span>shoe<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{<!-- --></span>path<span class="token operator">:</span><span class="token string">'/shopping/huawei'</span><span class="token punctuation">,</span> component<span class="token operator">:</span>huawei<span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{<!-- --></span>path<span class="token operator">:</span><span class="token string">'/zoo'</span><span class="token punctuation">,</span> component<span class="token operator">:</span>zoo<span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> el<span class="token operator">:</span> <span class="token string">'#app'</span><span class="token punctuation">,</span> router <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</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
- 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