vue - 路由器router
router的基本使用:
main.js
import Vue from 'vue' import App from './App' //导入VueRouter import VueRouter from 'vue-router' //导入自己写的router import router from './router/index' //使用VueRouter Vue.use(VueRouter) new Vue({ el: '#app', render: h=>h(App), //使用自己写的router router: router })
router/index.js
import Router from "vue-router"; import About from "../pages/About"; import Home from "../pages/Home"; export default new Router({ routes: [ { path: '/home', component: Home }, { path: '/about', component: About } ] })
组件:pages/
about: <template> <h1>ABOUT</h1> </template> <script> export default { name: 'About' } </script> <style> </style> home: <template> <h1>HOME</h1> </template> <script> export default { name: 'Home' } </script> <style> </style>
使用:
<template> <div> <div class="row"> <div class="col-xs-offset-2 col-xs-8"> <div class="page-header"><h2>Vue Router Demo</h2></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" to="/home">Home</router-link> <router-link class="list-group-item" to="/about">About</router-link> </div> </div> <div class="col-xs-6"> <div class="panel"> <div class="panel-body"> <!--组件在这里显示--> <router-view></router-view> </div> </div> </div> </div> </div> </template> <script> export default { components: { } } </script> <style> </style>
嵌套路由:
{ path: "/user", component: U, name: "User", children: [ // 子路由,路由嵌套 { path: "info", component: UI, name: "UserInfo", children: [ { path: "address", component: UA, name: "UserAddress" } ] } ] }
路由的query参数:
传递参数
<!-- 跳转并携带query参数,to的字符串写法 --> <router-link :to="/home/message/detail?id=666&title=你好">跳转</router-link> <!-- 跳转并携带query参数,to的对象写法 --> <router-link :to="{ path:'/home/message/detail', query:{ id:666, title:'你好' } }" >跳转</router-link>
接收参数
$route.query.id
$route.query.title
(32条消息) 路由传参的三种方式(query/params)_么心么肺的博客-CSDN博客_query传参
Vue路由传参详解(params 与 query) - 知乎 (zhihu.com)
(32条消息) Vue-router路由的props配置_李公子丶的博客-CSDN博客
(32条消息) 【vue】vue路由缓存的几种方式_没有梦想的coder的博客-CSDN博客_vue路由缓存
Vue 路由 导航守卫(全局守卫、路由独享守卫、组件内守卫) - 简书 (jianshu.com)