
步骤一:安装vue-router
npm install vue-router --save步骤二:在模块化工程中使用它(因为是一个插件,所以可以通过Vue.use()来安装路由功能)
<router-link>:该标签是一个vue-router中已经内置的组件,它会被渲染成一个<a>标签
<router-view>:该标签会根据当前路径,渲染出不同的组件
网页中其他的内容,比如顶部的标题/导航,或者底部的一些版权信息会和<router-view>出于同一个等级
在路由切换时,切换的是<router-view>挂载的组件,其他内容不会发生改变index.js的export中添加
mode: 'history'tag属性:渲染为指定元素
<router-link to="/home" tag="button">首页</router-link>replace属性:该属性不会留下history记录,所以指定replace的情况下,后退键返回不能返回到上一个页面中
<router-link to="/home" tag="button" replace>首页</router-link>active-class:当<router-link>对应的路由匹配成功时,会自动给当前元素设置一个router-link-active的class,设置active-class可以修改默认的名称
router-link-active属性修改为active
<router-link to="/home" tag="button" replace active-class="active">首页</router-link><button @click="homeClick">首页</button>methods: {
  homeClick() {
    // 通过代码的范式修改路径 vue-router
    this.$router.push('/home')
    console.log('homeClick');
  }
}{
  path: '/user/:userId',
  name: 'User',
  component: User
}子路由获取参数
<template>
  <div>
    <h2>我是用户界面</h2>
    <p>用户信息</p>
    <h2>{{ userId }}</h2>
    <h2>{{ $route.params.userId }}</h2>
  </div>
</template>
<script>
  export default {
    name: "User",
    computed: {
      userId() {
        return this.$route.params.userId
      }
    }
  }
</script>路由懒加载做了什么
index.js
方式一:
const Home = resolve => {
  require.ensurre(['../components/Home.vue'],()=>{
    resolve(require('../components/Home.vue'))
  })
}方式二:AMD写法
const About = resolve => require(['../compontents/About.vue'], resolve);方式三:在ES6中,我们可以有更加简单的写法来组织Vue异步组件和Webpack的代码分割
// 懒加载方法导入(一个懒加载对应一个js文件)
const Home = () => import('../components/Home')Home.vue
<router-link to="/home/news">新闻</router-link>
<router-link to="/home/message">消息</router-link>
<router-view></router-view>index.js
{
  path: '/home',
  name: 'Home',
  component: Home,
  children: [
    {
      path: '',
      redirect: '/news'
    },
    {
      path: 'news',
      component: HomeNews
    },
    {
      path: 'message',
      component: HomeMessage
    },
  ]
},button和router-link方法二选一
http://localhost:8080/profile?id=1&name=charles
# router-link
<router-link :to="{ path: '/profile',query:{id:'1',name:'charles'}}">个人资料</router-link>
# button
<button @click="userClick">用户</button>
<button @click="profileClick">个人资料</button>
<router-view></router-view>
userClick() {
  this.$router.push('/user/' + this.userId)
},
profileClick() {
  this.$router.push({
    path: '/profile',
    query: {
      id: 2,
      name: 'tom'
    }
  })
}profile.vue
<h2>{{ $route.query.id }}</h2>
 <h2>{{ $route.query.name }}</h2>// 前置守卫(guard)
router.beforeEach((to, from, next) => {
  document.title = to.matched[0].meta.title
  next()
})
// 后置钩子(hook)
router.afterEach((to,from)=>{
  
})index.js
{
  path: '/about',
  name: 'About',
  meta: {
    title: '关于'
  },
  component: About
},About.vue
<script>
  export default {
    name: "Home",
    created() {  // 创建组件
      console.log('created');
      // document.title = '首页'
      document.title = to.matched[0].meta.title
    },
    // mounted() {  // 挂载到dom
    //   console.log('mounted');
    // },
    // updated() { // 界面发生刷新
    //   console.log('updated');
    // }
  }
</script>(1)include:字符串或正则表达式,只有匹配的组件会被缓存
(2)exclude:字符串或正则表达式,任何匹配的组件都不会被缓存
keep-alive里面,所有路径匹配到的视图组件都会被缓存// 这两个函数,只有该组件被保持了装填使用keep-alive时,才是有效的
activated() {
  this.$router.push(this.path)
},
beforeRouteLeave(to, from, next) {
  this.path = this.$route.path;
  next()
}<keep-alive>
  <router-view/>
</keep-alive>原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。