Vue路由的使用

一 Vue路由使用

1. vue-router安装

若package中没有vue-router则安装

npm install vue-router 

2. vue-router实例文件组织

  1. /src/router/index.js
import VueRouter from 'vue-router'
import Vue from 'vue'
import home from '../components/home'
import about from '../components/about'

//使用插件
Vue.use(VueRouter)
//创建router对象
const routes = [
  {
    path: '',
    //redirect重定向
    redirect: '/home'
  },
  {
    path: '/home',
    component: home,
  },
  {
    path: '/about',
    component: about,
  }
]
const router = new VueRouter({
  //存放路由与组件的映射关系
  routes,
  //url去掉hash样式#
  mode: 'history',
  linkActiveClass: 'active'
  //统一修改标签激活类名
})
//导出
export  default router
  1. /src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router: router,
  //或者简写
  render: h=> h(App)
})
  1. /src/App.vue
<template>
  <div>
    <router-link to="/home" tag="button" replace>首页</router-link>
    <router-link to="/about" tag="button" replace>关于</router-link>
    <router-view></router-view>
    <h2>看看是不是同一层级</h2>
  </div>
</template>

<script>

export default {
  name: 'App',
}
</script>

<style>
  .active{
    color:#f00;
  }
</style>
  1. /src/components/home.vue
<template>
  <div>
    <h1>首页</h1>
  </div>
</template>

<script>
  export default {
    name: "home"
  }
</script>

<style scoped>

</style>
  1. /src/components/about.vue
<template>
  <div>
    <h1>关于</h1>
  </div>
</template>

<script>
  export default {
    name: "about"
  }
</script>

<style scoped>

</style>
  1. index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>router</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

3. vue-router相关标签

1. router-link

<router-link to="/home" tag="button" replace  active-class="active">首页</router-link>
//router-link标签默认会被解析为a标签
//但可以通过tag属性进行指定
//to属性为要跳转的组件
//replace指定页面中组件切换模式(组件切换后浏览器左上角的< > 不可用)
//active-class指定点击后标签添加的类名
//想统一修改标签激活后添加的类名,可以在/src/router/index.js文件router实例中添加linkActiveClass: 'active'

2. router-view

<router-view></router-view>
<h2>看看是不是同一层级</h2>//同一层级
//跳转的组件在DOM中的位置,与其他标签在同一层级

4. vue-router代码跳转

<template>
  <div id="app">
    <button @click="homeClick">首页</button>
    <button @click="aboutClick">关于</button>
    <router-view></router-view>
    <h2>看看是不是同一层级</h2>
  </div>
</template>

<script>

export default {
  name: 'App',
  methods: {
    homeClick(){
      //通过代码方式修改路由vue-router
      //this.$router.push('/home')
      this.$router.replace('/home')
    },
    aboutClick(){
      this.$router.push('/about')
    }
  }
}
</script>

<style>
  .active{
    color:#f00;
  }
</style>

5. vue-router动态路由

  1. /src/router/index.js
import VueRouter from 'vue-router'
import Vue from 'vue'
import home from '../components/home'
import about from '../components/about'
import user from '../components/user'
//使用插件
Vue.use(VueRouter)
//创建router对象
const routes = [
  {
    path: '',
    //redirect重定向
    redirect: '/home'
  },
  {
    path: '/home',
    component: home,
  },
  {
    path: '/about',
    component: about,
  }
  //添加 :id 用id接收传过来的参数
  {
    path: '/user/:id',
    component: user
  }
]
const router = new VueRouter({
  //存放路由与组件的映射关系
  routes,
  //url去掉hash样式#
  mode: 'history',
  linkActiveClass: 'active'
  //统一修改标签激活类名
})
//导出
export  default router
  1. /src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router: router,
  //或者简写
  render: h=> h(App)
})
  1. /src/App.vue
<template>
  <div id="app">
    <router-link to="/home" tag="button" replace  active-class="active">首页</router-link>
    <router-link to="/about" tag="button" replace active-class="active">关于</router-link>
    //添加用户router 数据来源userId
    <router-link :to="'/user/' + userId">用户</router-link>
    <router-view></router-view>
    <h2>看看是不是同一层级</h2>
  </div>
</template>

<script>

export default {
  name: 'App',
  data() {
    return{
      userId: 'zhangsan'
    }
  }
}
</script>

<style>
  .active{
    color:#f00;
  }
</style>

  1. /src/components/user.vue
<template>
  <div>
    <h2>用户为 {{userId}}</h2>
    <h2>或者 {{$route.params.id}}</h2>
  </div>
</template>

<script>
  export default {
    name: "user",
    computed: {
      userId() {
      //************注意是 route **********
      //获得route传过来的参数
        return this.$route.params.id
      }
    }
  }
</script>

<style scoped>

</style>

6. vue-router懒加载

一个路由编译成一个文件,解决打包后所有路由都在一个文件里导致加载事件过长
index.js

import VueRouter from 'vue-router'
import Vue from 'vue'

const home = () => import('../components/home')
const about = () => import('../components/about')
const user = () => import('../components/user')

//使用插件
Vue.use(VueRouter)
//创建router对象
const routes = [
  {
    path: '',
    //redirect重定向
    redirect: '/home'
  },
  {
    path: '/home',
    component: home,
    //component: () => import('../components/home')
  },
  {
    path: '/about',
    component: about,
  },
  {
    path: '/user/:id',
    component: user
  }
]
const router = new VueRouter({
  //存放路由与组件的映射关系
  routes,
  //url去掉hash样式#
  mode: 'history',
  linkActiveClass: 'active'
})
//导出
export  default router

7. vue-router子路由

  1. homeChild.vue
<template>
  <div>
    <h1>一个开心的子组件</h1>
  </div>
</template>

<script>
  export default {
    name: "homeChild"
  }
</script>

<style scoped>

</style>
  1. home.vue
<template>
  <div>
    <h1>首页</h1>
    <router-link to="/home/child">子组件</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
  export default {
    name: "home"
  }
</script>

<style scoped>

</style>
  1. index.js
import VueRouter from 'vue-router'
import Vue from 'vue'

const home = () => import('../components/home')
const homeChild = () => import('../components/homeChild')
const about = () => import('../components/about')
const user = () => import('../components/user')

//使用插件
Vue.use(VueRouter)
//创建router对象
const routes = [
  {
    path: '',
    //redirect重定向
    redirect: '/home'
  },
  {
    path: '/home',
    component: home,
    //component: () => import('../components/home')
    children: [
      {
        path: 'child',//子组件这里不加/
        component: homeChild,
      }
    ]
  },
  {
    path: '/about',
    component: about,
  },
  {
    path: '/user/:id',
    component: user
  }
]
const router = new VueRouter({
  //存放路由与组件的映射关系
  routes,
  //url去掉hash样式#
  mode: 'history',
  linkActiveClass: 'active'
})
//导出
export  default router

8. vue-router参数传递

  1. profile.vue
<template>
  <div>
    <h1>这是个开心的profile</h1>
    <h2>{{$route.query}}</h2>
    <h2>{{$route.path}}</h2>
  </div>
</template>

<script>
  export default {
    name: "profile"
  }
</script>

<style scoped>

</style>

  1. App.vue
<template>
  <div id="app">
    <router-link to="/home" tag="button" replace  active-class="active">首页</router-link>
    <router-link to="/about" tag="button" replace active-class="active">关于</router-link>
    <router-link :to="'/user/' + name">用户</router-link>
    <router-link :to="{path: '/profile', query:{name: 'pro'}}">档案</router-link>
    <router-view></router-view>
    <h2>看看是不是同一层级</h2>
  </div>
</template>

<script>

export default {
  name: 'App',
  data() {
    return{
      name:'si',
      userId: 'zhangsan',
    }
  }
}
</script>

<style>
  .active{
    color:#f00;
  }
</style>
  1. index.js
import VueRouter from 'vue-router'
import Vue from 'vue'

const home = () => import('../components/home')
const homeChild = () => import('../components/homeChild')
const about = () => import('../components/about')
const user = () => import('../components/user')
const profile = () => import('../components/profile')
//使用插件
Vue.use(VueRouter)
//创建router对象
const routes = [
  {
    path: '',
    //redirect重定向
    redirect: '/home'
  },
  {
    path: '/home',
    component: home,
    //component: () => import('../components/home')
    children: [
      {
        path: 'child',//子组件这里不加/
        component: homeChild,
      }
    ]
  },
  {
    path: '/about',
    component: about,
  },
  {
    path: '/user/:id',
    component: user
  },
  {
    path: '/profile',
    component: profile
  }
]
const router = new VueRouter({
  //存放路由与组件的映射关系
  routes,
  //url去掉hash样式#
  mode: 'history',
  linkActiveClass: 'active'
})
//导出
export  default router

9. vue-router导航守卫

index.js
先给路由加上meta(元数据)
再统一配置 router.beforeEach

import VueRouter from 'vue-router'
import Vue from 'vue'

const home = () => import('../components/home')
const homeChild = () => import('../components/homeChild')
const about = () => import('../components/about')
const user = () => import('../components/user')
const profile = () => import('../components/profile')
//使用插件
Vue.use(VueRouter)
//创建router对象
const routes = [
  {
    path: '',
    //redirect重定向
    redirect: '/home',
  },
  {
    path: '/home',
    component: home,
    //component: () => import('../components/home')
    children: [
      {
        path: 'child',//子组件这里不加/
        component: homeChild,
      }
    ],
    meta: {
      title:'首页'
    }
  },
  {
    path: '/about',
    component: about,
    meta: {
      title:'关于'
    }
  },
  {
    path: '/user/:id',
    component: user,
    meta: {
      title:'用户'
    }
  },
  {
    path: '/profile',
    component: profile,
    meta: {
      title:'档案'
    }
  }
]
const router = new VueRouter({
  //存放路由与组件的映射关系
  routes,
  //url去掉hash样式#
  mode: 'history',
  linkActiveClass: 'active'
})

router.beforeEach((to,from,next)=>{
  document.title = to.matched[0].meta.title
  next()//必须调用,否则路由无法跳转
})
//导出
export  default router

10. vue-router keep-alive(使跳转不被销毁)

App.vue

<template>
  <div id="app">
    <router-link to="/home" tag="button" replace  active-class="active">首页</router-link>
    <router-link to="/about" tag="button" replace active-class="active">关于</router-link>
    <router-link :to="'/user/' + name">用户</router-link>
    <router-link :to="{path: '/profile', query:{name: 'pro'}}">档案</router-link>
<!--将 router-view 放在keep-alive中 exclude表明除去哪些组件-->
    <keep-alive exclude="profile,user">
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

<script>

export default {
  name: 'App',
  data() {
    return{
      name:'si',
      userId: 'zhangsan',
    }
  }
}
</script>

<style>
  .active{
    color:#f00;
  }
</style>

11. 记录浏览状态

<template>
  <div>
    <h1>我是首页</h1>
    <router-link to="/home/news" tag="button">news</router-link>
    <router-link to="/home/detail" tag="button">detail</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
  export default {
    name: "home",
    data(){
      return {
        path:''
      }
    },
    activated() {
      this.$router.push(this.path)
    },
    beforeRouteLeave(to,from,next){
      this.path = this.$route.path
      next()
    }
  }
</script>

<style scoped>

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值