(五)VUE vue-router配置、导航、守卫

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
<style type="text/css">
	.router-link-active{
		color: green;
		font-weight: bold;
	}
</style>
</head>
<body>
	<div id="app">
	  <h1>Hello App!</h1>
	  <p>
		    <!-- 使用 router-link 组件来导航. -->
		    <!-- 通过传入 `to` 属性指定链接. -->
		    <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
		    <router-link to="/foo">Go to Foo</router-link>
		    <router-link to="/bar" replace>Go to Bar</router-link>
		    <router-link to="/userId/LIN">Go to My</router-link>
	  </p>
	  <!-- 路由出口 -->
	  <!-- 路由匹配到的组件将渲染在这里 -->
	  <router-view></router-view>
	</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js"></script>
<script type="text/javascript">
	// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
	
	

	// 1. 定义(路由)组件。
	// 可以从其他文件 import 进来
	const Foo = { template: `<div>
								foo
								<router-link to="/foo/foochild1">大儿子</router-link>
		   						<router-link to="/foo/foochild2">二儿子</router-link>
								<hr/>
								<router-view></router-view>
							</div>`
	}
	const Bar = { template: '<div>bar</div>' }
	let my = `<div>
				my
				{{$route.params.nameaaa}}
			  </div>`;
	const My = { 
		template:  my,
		created(){
			console.log(this.$route)//输出获取到的路由对象
			//提一个坑,当在当前组件中切换动态参数时组件被复用,钩子函数不会执行,比如/userId/xiaoming跳到/userId/xiaozhao时,解决方法:watch $route,或使用Router2.2中引入的beforeRouteUpdate钩子函数(Router中叫守卫)
		},
		watch:{
			'$route'(to, from){
				console.log(to)
				//to是将要切换过去的那个路由对象,from代表从哪个路由对象切换过来的
			}
		}
	}
	const FooChild1 = { template: `<div>foo的1儿子 Eldest child</div>` }
	const FooChild2 = { template: `<div>
									foo的2儿子 second-born
									<button @click="gotoUser">点击跳转到Bar</button>
								   </div>` ,
						methods:{
							gotoUser(){
								//js的路由导航 
        	            		// $router 路由实例 ,已经将实例注入到 Vue.prototype.$router
        	            		//this.$router.push('/user/xiaolong')//有历史记录的,可以传params和query参数
        	            		//this.$router.push({path:'/user/linge'})命名路由可以下面这样配置跳转,My组件中通过this.$route就能获取到
        	            		this.$router.push({name:'b', params:{nameaaa: 'linge'}, query:{age: 18}});
        	            		
        	            		// router.go(-1)//返回上一个历史记录(返回上一页)
							}
						}
	}

	// 2. 定义路由
	// 每个路由应该映射一个组件。 其中"component" 可以是
	// 通过 Vue.extend() 创建的组件构造器,
	// 或者,只是一个组件配置对象。
	// 我们晚点再讨论嵌套路由。
	const routes = [
	  { path: '/foo', component: Foo,
	  		children: [//定义子路由,注意的是,要在父组件中定义<rouetr-view></router-view>
	  			{path: 'foochild1', component: FooChild1},
	  			{path: 'foochild2', component: FooChild2}
	  		]
	   },
	  { path: '/bar', component: Bar },
	  { path: '/userId/:nameaaa', component: My, name: 'b'},//命名路由,b就代表path
	  { path: '/', redirect: '/foo/FooChild2' }//重定向
	]

	// 3. 创建 router 实例,然后传 `routes` 配置
	// 你还可以传别的配置参数, 不过先这么简单着吧。
	const router = new VueRouter({
	  routes // (缩写)相当于 routes: routes
	})
	router.beforeEach((to, from, next)=>{//全局路由前置守卫
		//next控制是否能切换的函数,如果调用了beforeEach钩子函数而在函数中不调用next的话不能切换任何路由
		next()//可以传参数执行跳转,但是要放在条件语句中执行,不然死循环,可以用在判断页面打开静止多少时间了,到一定时间让页面跳转
	})
	router.afterEach((to, from)=>{/*...*/})//全局路由后置守卫
	// 路由独享守卫beforeEnter:
		/*const routes = [{
						path: 'foo',
		 				component: Foo,
						beforeEnter: (to, from, next)=>{
							//与全局前置首位方法参数一样
						}
		}]*/
	// 组件内部守卫:
		/*beforeRouteEnter(to, from, next){//最早执行的钩子,访问不到组件实例this,要在异步中用next(Callback),在回调中第一个参数==组件实例this
		}
		beforeRouteUpdate(to, from, next){//通组件切换被复用时,这个钩子会被执行,可以访问组件实例this

		}
		beforeRouteLeave(to, from, next){//导航离开该组件对应的路由时执行,可以访问组件实例this

		}*/

	// 4. 创建和挂载根实例。
	// 记得要通过 router 配置参数注入路由,
	// 从而让整个应用都有路由功能
	const app = new Vue({
	  router
	}).$mount('#app')//相当于在配置项中声明el:"#app"

	// 现在,应用已经启动了!


//获取动态参数:
	//$route,称为路由对象,会被注入到每一个路由当前匹配的组件实例当中去,在该组件中this.$route包含了当前路由的信息包括path,params,query等等。。
</script>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值