赞
踩
一个页面可以配置多个router-view,加上name属性使之区分,在路由配置中需要将component变为components,代码如下
- <template>
- <div id="app">
- <div id="nav">
- <router-link to="/">Home</router-link> |
- <router-link to="/about">About</router-link>
- </div>
- <router-view/>
- <router-view name="compA"></router-view>
- <router-view name="compB"></router-view>
- </div>
- </template>
- import Vue from 'vue'
- import VueRouter from 'vue-router'
-
- Vue.use(VueRouter)
-
- const routes = [
- {
- path: '/',
- name: 'Home',
- components: {
- default: () => import('../views/Home.vue'),
- compA: () => import('../components/ComponentOne.vue'),
- compB: () => import('../components/ComponentTwo.vue')
- }
- },
- {
- path: '/about',
- name: 'About',
- components: {
- compA: () => import('../components/ComponentThree.vue'),
- compB: () => import('../components/ComponentTwo.vue')
- }
- }
- ]
-
- const router = new VueRouter({
- mode: 'history',
- base: process.env.BASE_URL,
- routes
- })
-
- export default router
以上就是多个router-view组合的基本使用
主要区分query查询模式和params,至于编程式导航$router.push和声明式router-link中区别的基本写法这里不做记录
query和path组合,参数是拼接在url后面,刷新页面参数不会丢失
<router-link :to="{ path: '/about', query: { id: 1111}}">About</router-link>
params和name组合,参数不显示在url中,刷新页面参数会丢失
<router-link :to="{ name: 'Home', params: { id: 2222}}">Home</router-link>
刷新前:
刷新后:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。