Vue-router路由使用


1、创建路由

import Vue from 'vue'
import VueRouter from 'vue-router'
//1 注册插件 Vue.use(VueRouter) const routes
= [ { path: '', redirect: '/home' }, { path: '/home', component: Home, meta: { title: '首页' }, children: [ { path: '', redirect: 'news' }, { path: 'news', component: HomeNews }, { path: 'message', component: HomeMessage } ] }, { path: '/about/:id', component: About, meta: { title: '关于' } }, { path: '/profile', component: Profile, meta: { title: '档案' } } ] //2 创建路由 const router = new VueRouter({ routes, mode: 'history' }) //3 导出路由 然后再入口文件 进行注册 export default router

2、配置路由

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

const Home = () => import('../components/Home')
const About = () => import('../components/About')
const Profile = () => import('../components/Profile')
const HomeNews = () => import('../components/HomeNews')
const HomeMessage = () => import('../components/HomeMessage')

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '',
        redirect: 'news'
      },
      {
        path: 'news',
        component: HomeNews
      },
      {
        path: 'message',
        component: HomeMessage
      }
    ]
  },
  {
    path: '/about/',
    component: About,
    meta: {
      title: '关于'
    }
  },
  {
    path: '/profile',
    component: Profile,
    meta: {
      title: '档案'
    }
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})


export default router

3、在App.vue进行设置

首页
   关于
   

4、路由跳转





5、路由传参有2中方式   params和query

5.1 params传递  分为几步     

第一步: 配置路由格式   (这里的id并不是唯一,你也可以为其定义aaa,bbbb等等 都可以)

 path: '/about/:id',
 component: About,

第二步




效果如下:

而且我们也可以在关于当前页面拿到这个id  格式如下: