vue-router使用
yarn add vue-router@next --dev
, 参数说明packageName@tag
orpackageName@version
创建router/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import { createRouter, createWebHistory } from "vue-router"; import ShowList from "../components/ShowList.vue" const router = createRouter({ history: createWebHistory(), routes: [ { path: "/show", component: ShowList }, { path: "/login", name: "Login", meta: { title: '登录' }, component: () => import ( "../views/Login.vue") },{ path: '/user/:id', components: { default: User, sidebar: Sidebar }, props: { default: true, sidebar: false } } ] })
router.beforeEach((to, from, next) => { document.title = `${to.meta.title} | vue-manage-system`; const role = localStorage.getItem('ms_username'); if (!role && to.path !== '/login') { next('/login'); } else if (to.meta.permission) { role === 'admin' ? next() : next('/403'); } else { next(); } }); export default router
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <template> <div id="app"> <div> // 下面的叫声明式 <router-link to="/">Home</router-link> | <router-link to="/show">show</router-link>| <router-link to="/hello">hello</router-link>| <router-link :to="{name: 'antd', params: { id: 2 }}">antd</router-link> </div> <router-view/> </div> </template>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| function submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { this.$router.push({name: "Mainwithout", params: {name: this.form.name}}) sessionStorage.setItem("isLogin", true) this.$store.dispatch("asyncUpdateUser", {name: this.form.name}) console.log("enter", this.$store.state) } else { this.$message.error('错了哦,这是一条错误消息'); return false; } }); } import {useRouter} from 'vue-router' setup(props,ctx){ let router = useRouter(); let start = () => { router.push({ path:'/home', query:{ num:1 } }) } onMounted(() => { const id = router.params.id }) }
|
调用方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| router.push('home')
router.push({ path: 'home' })
router.push({ name: 'user', params: { userId: '123' }})
router.push({ path: 'register', query: { plan: 'private' }})
router.replace(location, onComplete?, onAbort?)
router.go(n)
|
区分router和route
$router
: 是路由操作对象,只写对象
$route
: 路由信息对象,只读对象
1 2 3 4 5 6 7 8 9 10 11 12
| this.$router.push({ name:'hello', params:{ name:'word', age:'11' } })
this.name = this.$route.params.name; this.age = this.$route.params.age;
|
区分query和params
query传参要用path来引入,params传参要用name来引入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
this.$router.push({ name:'second', query: { queryId:'20180822', queryName: 'query' } })
this.$router.push({ path:'second', query: { queryId:'20180822', queryName: 'query' } })
this.queryName = this.$route.query.queryName; this.queryId = this.$route.query.queryId;
this.$router.push({ name:'second', params: { id:'20180822', name: 'query' } })
this.id = this.$route.params.id ; this.name = this.$route.params.name ;
{ path: '/second/:id/:name', name: 'second', component: () => import('@/view/second') }
|
-
params是路由的一部分, 必须要在路由后面添加参数名。query是拼接在url后面的参数,没有也没关系。
-
params一旦设置在路由,params就是路由的一部分,如果这个路由有params传参,但是在跳转的时候没有传这个参数,会导致跳转失败或者页面会没有内容。(最重要的一点,params刷新会消失。query则不会)
1 2
| parmas:https://blog.csdn.net/xxx query: https://blog.csdn.net?xxx
|
from:https://router.vuejs.org/zh/guide/essentials/dynamic-matching.html
跳转与视图
router-link 实现路由之间的跳转,如点击show超链接则会跳转到http://localhost:8080/show
1 2 3 4 5 6 7 8 9 10 11
| <router-link to="home">Home</router-link>
<a href="home">Home</a>
<router-link :to="{ path: 'home' }">Home</router-link>
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
<router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>
|
视图:router-view 当路由的path 与访问的地址相符时,会将指定的组件内容替换该 router-view,如果不点击而是直接将URL换成http://localhost:8080/show
也会显示ShowList.vue的内容(为什么是ShowList.vue?是因为router/index.js中将path=''/show'
与ShowList.vue绑定了)
命名视图==》存在多个、多层视图
动态传参跳转
采坑:
1 2
| :to="{path:'invest/scattered',query: {productId: list.id}}" to="invest/scattered?productId=1"
|
冒号可以传动态的参数(也可以传静态的);不加冒号是静态的路径;动态路由也可以以静态路由的形式写,参数固定如:to="{name: 'antd', params: { id: 2 }}"
==>to="/antd/2"
;参数为变量:
1 2
| <router-link :to="{name: 'antd', params: { id: 2 }}">antd</router-link> <router-link :to="`/antd/${uid}`">antd</router-link>
|
更推荐指定url的写法,因为就不用考虑name还是path了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <router-link :to="`/antd?name=mrli`">antd</router-link> // 要跳转的 Antd.vue {{ $route.query.name }}
// App.vue <router-link :to="`/antd/3`">antd</router-link> // 要跳转的 Antd.vue {{ $route.params.id }} // 还需要router/index.js中定义, 不然$route.params.id无法将那个值对应到id上 { path: "/antd/:id", name: "antd", component: AntdCom },
|