VUE的使用,学会这些就足够了!!!(一)

VUE的使用,学会这些就足够了!!!(一)

Vue.js 是什么?

vue是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue 也完全能够为复杂的单页应用提供驱动。

一、vue实例

Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统

      
      
  • 1

1、在文件中引入vue.js,下面两种方式选择其中一种

<script type="text/javascript" src="../vue.js"></script>

<! 开发环境版本,包含了有帮助的命令行警告 >
<script src=“https://cdn.jsdelivr.net/npm/vue/dist/vue.js”></script>

  • 1
  • 2
  • 3
  • 4
  • 5

2、建立简单的vue实例对象

<div id="app">
	//调用vue中的数据
	<h1>{{message}}</h1>
	//下面的两种书写方式结果相同
	<button v-on:click="click()">click</button>
	<button @click="click()">click01</button>
</div>

<script>
var ve= new Vue({
el:’#app’,
data:{
message:‘hello’,
},
methods:{
click:function(){
alert(“你好,世界”);
}
}
});
</script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

二、vue常用指令

1、v-on:绑定事件监听器,事件的类型由参数决定
例如:v-on:事件名称
或者@事件名称
在这里插入图片描述
2、v-if/v-else-if/v-else判断指令:根据表达式的值,进行渲染
例如:

<div id="app">
			//isshow为true时,标签内的内容显示到页面
			<h1 v-if="isshow">{{isshow}}ok</h1>
</div>
<script>
			new Vue({
				el:'#app',
				data:{
					message:'hello',
					isshow:true,
				},
				methods:{
					click:function(){
						alert("你好,世界");
					}
				}
			});
		</script>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3、v-model:数据绑定指令(一般为表单输入绑定)

//v-model 指令在表单<input>、<textarea> 及 <select> 元素上创建双向数据绑定
<input type="text" v-model="message" />{{message}}
  • 1
  • 2
  • 3

文本框里的内容与外面的内容绑定
在这里插入图片描述
4、v-for:循环(可以多层嵌套显示和java中的for循环原理一致)
v-for格式:<li v-for=“item in items”>{{item}} </ li>
其中:item是被迭代数组元素的别名
items是源数据数组
使用 item in items 的特殊格式语法

<div id="demo">
	<ul>
		<li v-for="item in items">{{item}}</li>
		<li v-for="item in arr1">{{item}}</li>
		<li v-for="item in arr1.a">{{item}}</li>
	</ul>
</div>
<script>
			new Vue({
				el:'#demo',
				data:{
					items:[23,23,344,45],
					arr1:{"a":[1,2,3],"b":[2,2,3]},
				},
				methods:{
				}
			});
		</script>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行结果如图
在这里插入图片描述
5、v-bind:动态绑定属性
使用格式
v-bind:属性名=“data数据对象中的值”
简写
:属性名=“data数据对象中的值”

<styple>
.child{
	width: 50px;
	height: 50px;
	background-color: gray;
	color: black;
	}
</style>
<div id="app">
	<img v-bind:src="img" /> //动态绑定了imges属性,img改变了图片,那么img标签也会改变
	<div :class="{'child':isshow}">div6</div>//div区域的显示根据isshow的值来判定

</div>
<script>
new Vue({
el:’#app’,
data:{
isshow:true,
img:‘111.png’,
}
})
</script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

三、生命周期函数|钩子函数

1、生命周期函数
beforecreated:加一些loading事件
created:结束loading事件,还做一些初始化,实现函数自执行
mounted:发起后端请求,取回数据
接受页面之间传递的参数
子组件向父组件传递参数
2、下面用一个生命周期函数演示的实例

<div id="demo">
			<h1>{{message}}</h1>
			<button @click="changemessage()">changemessage</button>
</div>

<script>
var ve =new Vue({
el:’#demo’,
data:{
message:‘vue生命周期’,

				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				beforeCreate<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">group</span><span class="token punctuation">(</span><span class="token string">"------------beforecreate-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				created<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">group</span><span class="token punctuation">(</span><span class="token string">"------------created-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				beforeMount<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">group</span><span class="token punctuation">(</span><span class="token string">"------------beforemount-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				mounted<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">group</span><span class="token punctuation">(</span><span class="token string">"------------mounted-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				beforeUpdate<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">group</span><span class="token punctuation">(</span><span class="token string">"------------beforeupdate-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				updated<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">group</span><span class="token punctuation">(</span><span class="token string">"------------updated-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				beforeDestroy<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">group</span><span class="token punctuation">(</span><span class="token string">"------------destotry-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				destroyed<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">group</span><span class="token punctuation">(</span><span class="token string">"------------destroyed-----------"</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">'el:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$el<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">'data:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>$data<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">'message:'</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">)</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span>
				methods<span class="token punctuation">:</span><span class="token punctuation">{</span>
					changemessage<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>
						<span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token operator">=</span><span class="token string">'666'</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>

//销毁是在实例外面调用的
ve.$destroy()

  • 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

实际的效果
在这里插入图片描述
点击changemessage按钮
在这里插入图片描述

四、vue组件

借用官网的一张图。
在这里插入图片描述
1、为什么使用组件?
使用组件一方面可以不用去直接修改标签,另一方面也会优化页面显示
注意:组件在实例前面书写
2、全局组件

<div id="box">
	{{message}}
	<all></all>
</div>
<script>
	Vue.component("all",{
				template:'<div><h1>{{name}}</h1></div>',
				// template:'#box1',
				data:function(){
					return{
						name:'李四'
					}
				}
			})
	var ve = new Vue({
		el:"#box",
			data:{
				message:'hello'
			}
		});
</script>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3、局部组件:局部组件放在实例中

父子组件可以嵌套使用
父子组件间作用域相互独立
子组件只能在父组件的模板中进行调用

子组件是父组件细化拆分的过程
父组件向子组件传值通过props进行

下面的示例中包含了父组件向子组件传值的功能

<div id="box">
	<test>
	</test>
</div>
<template id='box1'>
	<div>
	<input type="text" v-model="message" />
	<box1 num="1" txt='yi' :msg="message"></box1>
	<box1 num="2" txt='er' :msg="message"></box1>
	</div>
</template> 

<script>
var ve = new Vue({
el:’#box’,
data:{
},
//父級
components:{
‘test’:{
template:"#box1",
data:function(){
return{
message:‘aaa’
}
},
//子级
components:{
‘box1’:{
template:"#box2",
//子级想将父级的一个值,作为自己的一个局部变量
data:function(){
return{
mymsg:this.msg
}
},
//计算属性
computed:{
mymsg1:function(){
return this.msg+’!’
}
},
props:[
‘num’,‘txt’,‘msg’
]
}
}
}
}
})
</script>

  • 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

在这里插入图片描述
4、自定义监听事件(子组件向父组件传值)
自定义监听事件: $emit()
传值的过程:
子组件设定了一个点击事件,点击事件中夹带着传递的值—》通过getval方法将拿到的值赋值给父组件的message—》父组件显示出message值

<div id="box">
			<parent></parent>
</div>
	<template id='par'>
		<div>
			<h1> 父组件:{{parentmsg}}||{{message}}</h1>
			<child @change="getval"></child>
		</div>
	</template>
	<template id='child'>
		<div @click="fn()">
			<h2>子组件:{{childmsg}}</h2>
		</div>
	</template>
<script>
	var vm = new Vue({
		el:"#box",
		components:{
			"parent":{
				template:'#par',
				data:function(){
					return {
						parentmsg:"父组件的信息",
						message:''
					}
				},
				methods:{
					getval:function(val){
						this.message = val
						console.log(val)
					}
				},
				components:{
					'child':{
						template:'#child',
						data:function(){
							return{
								childmsg:'子组件的信息'
							}
						},
						methods:{
							fn:function(){
								this.$emit('change',this.childmsg)
							}
						}
					}
				}
			}
		}
	})
</script>

 
 
  • 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

5、SLOT插槽(vue2.6.0之前版本使用的slot)
目的:其目的在于让组件的可扩展性更强,用来混合父组件的内容与子组件自己的模板.
分为匿名slot和具名slot

//匿名slot
<div id="box">
			<my-component>
		<span class="token operator">&lt;</span><span class="token operator">/</span>my<span class="token operator">-</span>component<span class="token operator">&gt;</span>

</div>
<template id=‘mycomponent’>
<div>
<p> 头部</p>
<slot> 如果没有分发,则默认显示</slot>
<p> 底部</p>
</div>
</template>
<script>
Vue.component(‘my-component’,{
template:’#mycomponent’

	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	Vue<span class="token punctuation">.</span><span class="token function">component</span><span class="token punctuation">(</span><span class="token string">'my-computer'</span><span class="token punctuation">,</span><span class="token punctuation">{</span>
		template<span class="token punctuation">:</span><span class="token string">'#mycomputer'</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token keyword">var</span> vm <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 punctuation">:</span><span class="token string">'#box'</span><span class="token punctuation">,</span>
		
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</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

在这里插入图片描述

//具名slot
<div id="box">
	<my-computer>
		<div slot='CPU'>n9000</div>
		<div slot='GPU'>OOOO</div>
	</my-computer>
</div>
<template id='mycomputer'>
	<div>
		<slot name="GPU">GPU</slot>
		<slot name="CPU">CPU</slot>
		<slot name="DIST">DIST</slot>
	</div>
</template>
<script>
		Vue.component('my-computer',{
			template:'#mycomputer'
		})
		var vm = new  Vue({
			el:'#box',
		})
	</script>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

五、vue的路由设置

要引入vue-router.js库文件
作用:
根据url锚点的位置,在容器中加载不同的模块,本质是作为页面导航
完成SPA(Singal Page Application)的开发
一种特殊的web应用,它将所有活动局限于一个web页面中,仅web页面初始化时加载项应的html、JavaScript、css
一旦页面加载完成,SPA不会因为用户的操作而进行页面的重新加载或跳转,而是利用JavaScript动态的变换HTML(采用的是div切换显示或隐藏),从而实现ui与用户的交互
下面是一个简单地单页面示例

<div id="app">
	<ul>
		<li >
			<router-link to="/home">首页</router-link>
		</li>
		<li >
			<router-link to="/news">新闻</router-link>
		</li>
		<li >
			<router-link to="/hot">热点</router-link>
		</li>
	</ul>
	<div class="show">
		<router-view></router-view>
	</div>
	<button @click="back()">go back</button>
</div>
<script>
		const Home={template:'<h2>首页</h2>'}
		const News={template:'<h2>新闻</h2>'}
		const Hot={template:'<h2>热点</h2>'}
		//Vue.extend(template:'<h1>首页</h1>')
		//配置路径
		const routes=[
			{
				path:'/home',component:Home
			},
			{
				path:'/news',component:News
			},
			{
				path:'/hot',component:Hot
			}
		]
	<span class="token comment">//创建router</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 punctuation">}</span><span class="token punctuation">)</span>
	
	<span class="token keyword">var</span> vm <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 punctuation">:</span><span class="token string">'#app'</span><span class="token punctuation">,</span>
		router<span class="token punctuation">,</span>
		beforeCreate<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>
			<span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">catch</span><span class="token punctuation">(</span>err<span class="token operator">=&gt;</span><span class="token punctuation">{</span>err<span class="token punctuation">}</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		methods<span class="token punctuation">:</span><span class="token punctuation">{</span>
			<span class="token function">back</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>$router<span class="token punctuation">.</span><span class="token function">go</span><span class="token punctuation">(</span><span class="token operator">-</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 punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

</script>

  • 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

二级路由的配置是在一级路由的基础上,在某一个path下再分离出几个低级的path
示例

{
	//新闻模块下有将新闻进行分类:科技、军事等;其他步骤不影响
	path:'/news',component:News,
	children:[
		/* 二级路由,path分配,前面没有“ / ” */
		{
			path:'js',
			component:{
				template:'<p>军事类新闻</p>'
			}
		},
		{
			path:'kj',
			component:{
				template:'<p>科技类新闻</p>'
			}
		},
		{
			/* 默认页面可以直接用一个组件,也可以重定向 */
			path:'/',
			redirect:'js'
			// component:{
			// 	template:'<p>默认值</p>'
			// }
		}
	]
},

 
 
  • 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

路由的进一步详解

六、axios的使用

使用了常用的两种axios的方法
在这里插入图片描述
axios的get方法使用

<div id="app">
<h1>{{msg}}</h1>
	<button @click='getMsg'>Click</button>
</div>

<script>
/* 创建Vue实例 /
var vm=new Vue({
el:’#app’,
data:{
msg:’’
},
methods:{
getMsg:function(){
/ 方式一
axios.get(’./text.txt?name=zhy&age=20’).then(res=>{
console.log(res.data)
this.msg=res.data
}).catch(err=>{
console.log(err)
}) */

//方式二
axios.get(’./text.txt’,{params:{name:‘zhy’,age:20}}).then(res=>{
this.msg=res.data
}).catch(err=>{
console.log(err)
})
}

	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</script>

  • 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

在这里插入图片描述
点击事件触发后,就会将text文件中的数据拿到前端显示
post和get方法使用一样

总结

有些点没有写到,之后会继续补充。

                                </div><div><div></div></div><div><div></div></div><div><div></div></div><div><div></div></div><div><div></div></div><div><div></div></div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
                            </div>
</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值