- import VueRouter from 'vue-router'
-
- import About from '../components/About'
-
- import Home from '../components/Home'
-
- //创建并暴露一个路由器
- export default new VueRouter({
- routes:[
- {
- path:"/about",
- component:About
- },
- {
- path:"/home",
- component:Home
- }
- ]
- })
使用
实现切换(active-class可配置高亮样式)
- <router-link class="list-group-item" active-class="active" to="/about">Aboutrouter-link>
指定显示的位置
- <router-view>router-view>
main.js
- import Vue from "vue"
-
- import App from "./App"
-
- import VueRouter from 'vue-router'
-
- import router from "./router/index"
-
- Vue.config.productionTip = false;
-
-
- Vue.use(VueRouter)
-
- new Vue({
- el: "#app",
- render: h => h(App),
- router:router
- })
router/index.js
- import VueRouter from 'vue-router'
-
- import About from '../components/About'
-
- import Home from '../components/Home'
-
- //创建并暴露一个路由器
- export default new VueRouter({
- routes:[
- {
- path:"/about",
- component:About
- },
- {
- path:"/home",
- component:Home
- }
- ]
- })
App.vue
- <template>
- <div>
- <div>
- <div class="row">
- <div class="col-xs-offset-2 col-xs-8">
- <div class="page-header"><h2>Vue Router Demoh2>div>
- div>
- div>
-
- <div class="row">
- <div class="col-xs-2 col-xs-offset-2">
- <div class="list-group">
-
-
-
- <router-link class="list-group-item" active-class="active" to="/about">Aboutrouter-link>
- <router-link class="list-group-item" active-class="active" to="/home">Homerouter-link>
- div>
- div>
-
- <div class="col-xs-6">
- <div class="panel">
- <div class="panel-body">
-
- <router-view>router-view>
- div>
- div>
- div>
- div>
- div>
- div>
- template>
-
- <script>
-
- import About from "./components/About"
- import Home from "./components/Home"
-
- export default {
- name: "App",
- components: {
- About,
- Home
- },
- };
- script>
-
- <style>
- style>
About.vue与Home.vue
- <template>
- <h2>我是About的内容h2>
- template>
-
- <script>
- export default {
- name:"About"
- }
- script>
-
- <style>
-
- style>
-
- --------------------------------------------------------------------------------
-
- <template>
- <h2>我是Home的内容h2>
- template>
-
- <script>
- export default {
- name:"Home"
- }
- script>
-
- <style>
-
- style>
- export default new VueRouter({
- routes:[
- {
- path:"/about",
- component:About
- },
- {
- path:"/home",
- component:Home,
- //通过children配置子路由
- children:[
- {
- //此处一定不要写:/news
- path:"news",
- component:News
- },
- {
- path:"message",
- component:Message
- }
- ]
- }
- ]
- })
调转(要写完整路径)
<router-link to='/home/news'>Newsrouter-link>
下面的代码通过query来传递参数
- <router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{ m.title }}router-link>
-
- <template>
- <ul>
- <li>消息编号:{{$route.query.id}}li>
- <li>消息标题:{{$route.query.title}}li>
- ul>
- template>
- <router-link
- :to="{
- path: '/home/message/detail',
- query: {
- id: m.id,
- title: m.title,
- },
- }">
- {{ m.title }}
- router-link>
-
- <template>
- <ul>
- <li>消息编号:{{$route.query.id}}li>
- <li>消息标题:{{$route.query.title}}li>
- ul>
- template>
- children:[
- {
- name:"xin", //给路由起名字
- path:"detail",
- component:Detail
- }
- ]
-
-
- <router-link
- :to="{
- //跳转到指定名字的路由
- name: 'xin',
- query: {
- id: m.id,
- title: m.title,
- },
- }">
- {{ m.title }}
- router-link>
-
-
- <router-link
- :to="{
- //跳转到指定名字的路由
- name: 'xin'}">
- {{ m.title }}
- router-link>
注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置。
配置路由接收params参数
- routes:[
- {
- //使用params跳转必须要用name属性进行跳转
- name:"hahaha",
- //使用占位符接收params参数
- path:"detail/:id/:title"
- component:About
- }
- ]
-
- //------------------------------------------------------
-
- //跳转并携带params参数,to的字符串写法
- <router-link :to='/home/message/detail/666/标题'>跳转router-link>
-
- //跳转并携带params参数,to的对象写法
- <router-link :to='{
- name:"hahaha",
- params:{
- id:666,
- title:"你好"
- }
- }'>跳转router-link>
-
-
- //接收参数
- <h1>{{$route.params.id}}h1>
作用:让路由组件更方便的收到参数
- {
- name:"xiangqing",
- path:"detail:id",
- component:Detail
-
- //第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
- //props:{a:900}
-
- //第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detail组件
- //props:true
-
- //第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
- //为回调函数,参数为路由的$route
- props(route){
- return {
- id:route.query.id,
- title.route.query.title
- }
- }
- }
<router-link replace :to="{path: '/home/message/detail',query: {id: m.id,title: m.title,},}">{{ m.title }}router-link> 编程式路由导航:不借助
可以给按钮点击事件中添加下面五种代码。
- //以push的方式跳转
- pushShow(m){
- this.$router.push({
- name:"xiangqing",
- query:{
- id:m.id,
- title:m.title
- }
- })
- }
-
- //以replace的方式跳转
- replacShow(m){
- this.$router.replace({
- name:"xiangqing",
- query:{
- id:m.id,
- title:m.title
- }}
- )
-
- //后退一步
- back(){
- this.$router.back();
- }
-
- //前进一步
- forward(){
- this.$router.forward();
- }
-
- testGo(){
- //如果为正数向前前进n步,如果为负数向后后退n步
- this.$router.go(1);
- }
路由切换的时候会销毁当前的路由去创建显示新的路由,如果被销毁的路由中有输入框中有信息,信息也会被销毁。
keep-alive标签可以解决这种问题,默认是缓存所有的显示路由,可以通过include属性去设置对指定的组件进行缓存。
- <keep-alive>
- <router-view>router-view>
- keep-alive>
-
- <keep-alive include="News">
- <router-view>router-view>
- keep-alive>
-
- <keep-alive :include="['News','Mwssage']">
- <router-view>router-view>
- keep-alive>
路由组件所独有的两个生命钩子,用于捕获路由组件的激活状态,与moundted同级
当组件获取焦点时会自动调用:activated()
当组件失去焦点时会自动调用:deactivated()
作用:对路由进行权限控制
执行顺序:前置路由,后置路由,路由组件
- const router=new VueRouter({
- routes: [
- {
- path: "/about",
- component: About
- }
- ]
- })
-
- //全局前置路由守卫,初始化或者路由之前跳转时会触发
- //to:目标路由的信息,name属性为路由名
- //from:来源路由的信息,name属性为路由名
- //next:是否放行的函数,执行就放行
- router.beforeEach((to,from,next)=>{
- next(); //执行next函数放行
- })
- const router=new VueRouter({
- routes: [
- {
- path: "/about",
- component: About,
- //meta用于配置自定义属性
- meta:{isAuth:true}
- }
- ]
- })
-
- //使用时meta.isAuth
- //创建并暴露一个路由器
- const router = new VueRouter({
- routes: [
- {
- path: "/about",
- component: About,
- meta: { title: "about" },
- }
- ]
- })
-
- //全局后置路由守卫,before执行后执行
- router.afterEach((to, from) => {
- //跳转之后改变网页标题
- document.title = to.meta.title || '主页';
- })
某一个路由独享的守卫,用法与前置路由守卫一样。
- //创建并暴露一个路由器
- const router = new VueRouter({
- routes: [
- {
- path: "/about",
- component: About,
- meta: { title: "about" },
- beforeEnter(to,from,next){
- next(); //执行next放行
- }
- },
- ]
- })
写在组件配置项中与mounted同级。
用法与全局路由一致、
- //通过路由规则进入时调用
- beforeRouteEnter(to,from,next){
- }
-
- //通过路由规则离开时调用
- beforeRouteleave(to,from,next){
- }
