vue 基于abstract 路由模式 實(shí)現(xiàn)頁面內(nèi)嵌的示例代碼
abstract 路由模式
abstract 是vue路由中的第三種模式,本身是用來在不支持瀏覽器API的環(huán)境中,充當(dāng)fallback,而不論是hash還是history模式都會(huì)對(duì)瀏覽器上的url產(chǎn)生作用,本文要實(shí)現(xiàn)的功能就是在已存在的路由頁面中內(nèi)嵌其他的路由頁面,而保持在瀏覽器當(dāng)中依舊顯示當(dāng)前頁面的路由path,這就利用到了abstract這種與瀏覽器分離的路由模式。
路由示例
export const routes = [
{
path: "/",
redirect: "abstract-route",
},
{
path: "/embed-route",
name: "embedded",
component: () =>
import(/* webpackChunkName: "embed" */ "../views/embed.vue"),
},
{
path: "/abstract-route",
name: "abstract",
component: () =>
import(/* webpackChunkName: "abstract" */ "../views/abstract.vue"),
},
];
const router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes,
});
本次示例有兩個(gè)路由,分別為abstract,embedded,其中abstract視圖上展開一個(gè)抽屜,抽屜當(dāng)中顯示embedded的視圖。即:
<template>
<div>
<RouterDrawer
:visible.sync="visible"
:path="{ name: 'embedded' }"
size="50%"
title="drawer comps"
></RouterDrawer>
<el-button @click="visible = true">open drawer</el-button>
</div>
</template>
// embedded views
<template>
<div>
embedded views
</div>
</template>

router-drawer 封裝
當(dāng)前項(xiàng)目默認(rèn)是history 的路由模式,因此在進(jìn)入abstract頁面時(shí),瀏覽器Url為http://127.0.0.1:8010/abstract-route,而router-drawer要做的是在此基礎(chǔ)上,重新實(shí)例化一個(gè)abstract模式的路由,然后在組件當(dāng)中利用<router-view />去掛載要被內(nèi)嵌的目標(biāo)頁面。即:
<template>
<el-drawer
:visible.sync="visible"
v-bind="$attrs"
:before-close="handleClose"
>
<router-view />
</el-drawer>
</template>
<script>
import { routes } from "../router/index";
import VueRouter from "vue-router";
export default {
name: "router-drawer",
props: {
path: {
type: Object,
required: true,
},
visible: {
type: Boolean,
required: true,
default: false,
},
},
// 此處實(shí)例化一個(gè)新的router來配合當(dāng)前頁面的router-view
router: new VueRouter({
mode: "abstract",
base: "/",
routes,
}),
methods: {
handleClose() {
this.$emit("update:visible", false);
},
},
mounted() {
console.log("drawer router", this.$router);
this.$router.push(this.path);
},
};
</script>
通過打印日志可以得出兩個(gè)實(shí)例化的路由:

這樣即可實(shí)現(xiàn)在不改變當(dāng)前頁面path的前提下加載其他路由中的views了。
以上就是vue 基于abstract 路由模式 實(shí)現(xiàn)頁面內(nèi)嵌的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于vue 實(shí)現(xiàn)頁面內(nèi)嵌的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解vue路由篇(動(dòng)態(tài)路由、路由嵌套)
這篇文章主要介紹了詳解vue路由篇(動(dòng)態(tài)路由、路由嵌套),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
VUE2.0+ElementUI2.0表格el-table循環(huán)動(dòng)態(tài)列渲染的寫法詳解
這篇文章主要介紹了VUE2.0+ElementUI2.0表格el-table循環(huán)動(dòng)態(tài)列渲染的寫法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Vue通過WebSocket建立長連接的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue通過WebSocket建立長連接的實(shí)現(xiàn)代碼,文中給出了問題及解決方案,需要的朋友可以參考下2019-11-11
vue實(shí)現(xiàn)可視化可拖放的自定義表單的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)可視化可拖放的自定義表單的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
ToB項(xiàng)目如何沉淀業(yè)務(wù)公共組件示例詳解
這篇文章主要為大家介紹了ToB項(xiàng)目如何沉淀業(yè)務(wù)公共組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

