具體功能:前端
在一些後臺系統中,見過相似的功能,是利用 iframe標籤添加連接的方式,實現這個功能。那麼在vue中怎麼去實現?vue
首先,這是一個單頁面網站應用,使用了vue-router進行前端路由,那麼咱們可使用 vue-router 的命名視圖,這種方式去代替iframe。
webpack
vue-router命名視圖官方代碼:git
<router-view class="view one"></router-view><router-view class="view two" name="a"></router-view><router-view class="view three" name="b"></router-view>複製代碼
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: Foo,
a: Bar,
b: Baz
}
}
]
})複製代碼
項目結構github
router代碼:web
import Vue from "vue";import Router from "vue-router";import store from '../store';Vue.use(Router);// layout頁面const Layout = () => import(/* webpackChunkName: "layout" */ "@/views/layout/Layout.vue");// 產品相關頁面const Product = () => import(/* webpackChunkName: "product" */ "@/views/product/Index.vue");const ProductDetail = () => import(/* webpackChunkName: "productDetail" */ "@/views/product/Detail.vue");// 新聞相關頁面const News = () => import(/* webpackChunkName: "news" */ "@/views/news/Index.vue");const NewsDetail = () => import(/* webpackChunkName: "newsDetail" */ "@/views/news/Detail.vue");const rotuer = new Router({ mode: "history", base: process.env.BASE_URL, routes: [ { path: "/", name: "layout", component: Layout, redirect: "/product", children: [ { path: "/product", name: "product", meta: { title: "產品", tabName: "產品列表" }, components: { default: Product, detail: ProductDetail } }, { path: "/news", name: "news", meta: { title: "新聞", tabName: "新聞列表" }, components: { default: News, detail: NewsDetail } } ] } ]});rotuer.afterEach(to => { // 初始化navTag標籤 if (to.meta && to.meta.tabName) { store.dispatch('navTab/addTab', { tabName: to.meta.tabName, initStatus: true }) }});export default rotuer;複製代碼
components: {
default: Product,
detail: ProductDetail,
create: ProductCreate
}複製代碼
layou.vue的部分代碼:vue-router
<template> <el-row class="container"> <el-col :span="24"> <Header @collapse="collapseHandler" :isCollapse="isCollapse"></Header> </el-col> <el-col :span="24" class="main"> <Menu :isCollapse="isCollapse"></Menu> <el-main class="content-container"> <nav-tab :navTabs="navTabs"></nav-tab> <div class="main-content"> <router-view v-if="!navTabs.length"></router-view> <div class="navTab-content" v-for="item in navTabs" :key="item.tabId" v-show="item.active"> <keep-alive> <router-view :name="item.vName"></router-view> </keep-alive> </div> </div> </el-main> </el-col> </el-row> </template>複製代碼
主要使用<router-view :name="item.vName"></router-view>,顯示命名的組件。vuex
如下是對navTab.js中的部分代碼說明:數組
一、state定義bash
const tabIdStr = "tab_"; // 標籤的id前綴
const state = {
tabs: [],
tabMaxNum: 0
};複製代碼
二、新增標籤
/**
* 添加標籤
* @param {Any} tabId tab的惟一id標識
* @param {String} tabName tab的標題
* @param {String} vName 對應router命名視圖的名稱
* @param {Object} pParams 參數傳遞
* @param {Boolean} initStatus 初始化狀態
*/
addTab({ commit, state }, { tabId, tabName, vName, pParams, initStatus = false }) {
// 設置標籤id
let newTabId = tabIdStr + (tabId || 0); // 默認 0
let opts = {
tabName: "",
vName: "default",
pParams: {},
active: true,
...{
tabId: newTabId,
tabName,
vName,
pParams
}
};
// 初始化時,重置標籤
if (initStatus) {
commit("resetTabs");
}
// 判斷函數
let hasTabId = item => {
return item.tabId === newTabId;
};
// 判斷新增標籤是否已存在,若是存在直接激活,不然新增
if (state.tabs.some(hasTabId)) {
// 激活標籤
commit("activeTab", newTabId);
return false;
}
// 添加標籤
commit("addTab", opts);
},複製代碼
// 初始化時,重置標籤
if (initStatus) {
commit("resetTabs");
}複製代碼
rotuer.afterEach(to => {
// 初始化navTag標籤
if (to.meta && to.meta.tabName) {
store.dispatch('navTab/addTab', {
tabName: to.meta.tabName,
initStatus: true
})
}
});複製代碼
const tabIdStr = "tab_"; // 標籤的id前綴
const state = {
tabs: [],
tabMaxNum: 0
};
const getters = {
getNavTabs: state => state.tabs
};
const actions = {
/**
* 添加標籤
* @param {Any} tabId tab的惟一id標識
* @param {String} tabName tab的標題
* @param {String} vName 對應router命名視圖的名稱
* @param {Object} pParams 參數傳遞
* @param {Boolean} initStatus 初始化狀態
*/
addTab({ commit, state }, { tabId, tabName, vName, pParams, initStatus = false }) {
// 設置標籤id
let newTabId = tabIdStr + (tabId || 0); // 默認 0
let opts = {
tabName: "",
vName: "default",
pParams: {},
active: true,
...{
tabId: newTabId,
tabName,
vName,
pParams
}
};
// 初始化時,重置標籤
if (initStatus) {
commit("resetTabs");
}
// 判斷函數
let hasTabId = item => {
return item.tabId === newTabId;
};
// 判斷新增標籤是否已存在,若是存在直接激活,不然新增
if (state.tabs.some(hasTabId)) {
// 激活標籤
commit("activeTab", newTabId);
return false;
}
// 添加標籤
commit("addTab", opts);
},
/**
* 切換標籤
* @param {String} tabId tab的惟一id標識
*/
changeTab({ commit }, { tabId }) {
// 激活標籤
commit('activeTab', tabId);
},
/**
* 更多標籤處理
* @param {Number} index tabs數組下標
*/
handleMoreTab({ commit }, { index }) {
commit('handleMoreTab', index);
},
/**
* 刪除標籤
* @param {Number} index tabs數組下標
*/
deleteTab({ commit }, { index }) {
commit('deleteTab', index);
},
/**
* 刪除其餘標籤
*/
deleteOtherTab({ commit, state }) {
// 保存第一個標籤
let firstTab = state.tabs[0];
// 若是第一個當前標籤是第一個,則直接刪除所有
if(firstTab.active) {
commit('deleteAllTab');
} else {
commit('deleteOtherTab');
}
},
/**
* 刪除所有標籤
*/
deleteAllTab({ commit }) {
commit('deleteAllTab');
}
};
const mutations = {
/**
* 添加標籤
*/
addTab(state, opts) {
// 隱藏其餘標籤狀態
state.tabs.forEach(item => {
item.active = false;
});
// 當tabs數量大於或等於標籤的最大顯示數,新添加的標籤放在可顯示的最後一位
if(state.tabs.length >= state.tabMaxNum) {
state.tabs.splice(state.tabMaxNum - 1, 0, opts);
} else {
state.tabs.push(opts);
}
},
/**
* 激活標籤
*/
activeTab(state, tabId) {
state.tabs.forEach(item => {
item.active = false;
if (item.tabId === tabId) {
item.active = true;
}
});
},
/**
* 更多標籤處理
*/
handleMoreTab(state, index) {
let tabs = state.tabs;
let _index = state.tabMaxNum + index;
// 激活點擊標籤
tabs[_index].active = true;
// 拷貝點擊標籤
let copyTab = [tabs[_index]];
// 刪除點擊標籤
tabs.splice(_index, 1);
// 隱藏其餘標籤
tabs.forEach(item => {
item.active = false;
});
// 插入到可顯示的標籤最後一個位置
tabs.splice([state.tabMaxNum - 1], 0, ...copyTab);
},
/**
* 刪除標籤
*/
deleteTab(state, index) {
let tabs = state.tabs;
// 判斷刪除的是當前標籤,需激活上一個標籤
if(tabs[index].active && tabs.length > 0) {
tabs[index -1].active = true;
}
tabs.splice(index, 1);
},
/**
* 刪除其餘標籤
*/
deleteOtherTab(state) {
// 解構第一個標籤,其餘標籤
let [firstTab, ...otherTabs] = state.tabs;
// 獲取當前標籤
let curTab = otherTabs.filter(item => item.active);
state.tabs = [firstTab, ...curTab];
},
/**
* 刪除所有標籤
*/
deleteAllTab(state) {
let tabs = state.tabs;
// 除了第一個標籤其餘的都刪除
let firstTab = tabs[0];
firstTab.active = true;
state.tabs = [firstTab];
},
/**
* 重置標籤
*/
resetTabs(state) {
state.tabs = [];
},
/**
* 設置顯示標籤最大值
*/
setMaxTabVal(state, val) {
state.tabMaxNum = parseInt(val);
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};複製代碼
部分代碼:
<template> <div class="nav-wrap"> <div class="nav-title"> <strong>{{$route.meta.title}}</strong> </div> <div class="nav-tabs" ref="tabsNav"> <div class="tabs-item" :class="{ 'acitve': item.active }" @click="handleClickTab(item)" v-for="(item, index) in navTabs.slice(0, this.tabMaxNum)" :key="item.tabId"> {{item.tabName}} <i class="el-icon-close icon-close" @click.stop="handleCloseTab(index)" v-if="index"></i> </div> <div class="more"> <div class="more-btn" @click="handleClickMore"> <i class="icon el-icon-arrow-down"></i> </div> <ul class="more-dropdown-menu" v-show="moreStatus"> <li @click.stop="handleClickMoreTab(index)" v-for="(item, index) in navTabs.slice(this.tabMaxNum)" :key="item.tabId"> <span>{{item.tabName}}</span> <i class="el-icon-close icon-close" @click.stop="handleCloseTab(item, index)"></i> </li> <li @click.stop="handleClickDelAll"> <span>關閉所有</span> </li> <li @click.stop="handleClickDelOther"> <span>關閉其餘</span> </li> </ul> </div> </div> </div></template><script>import { mapGetters } from 'vuex';export default { data() { return { tabMaxNum: 1, moreStatus: false } }, computed: { ...mapGetters({ navTabs: 'navTab/getNavTabs' }) }, mounted() { // 初始化 this.init(); window.addEventListener('resize', this.init, false); }, deactivated() { window.removeEventListener('resize', this.init, false); }, methods: { /** * 初始化 */ init() { // 計算標籤最大顯示個數 this.calcTabMaxNum(); }, /** * 計算標籤最大顯示個數 */ calcTabMaxNum() { if (!this.$refs.tabsNav) { return false; } let tabsNav = this.$refs.tabsNav; let tabsItem = tabsNav.querySelectorAll('.tabs-item'); let moreW = tabsNav.querySelector('.more').getBoundingClientRect().width; let navW = tabsNav.getBoundingClientRect().width - moreW; let itemW = tabsItem[0].getBoundingClientRect().width; // 設置最大值 this.tabMaxNum = Math.floor(navW / itemW); this.$store.commit('navTab/setMaxTabVal', this.tabMaxNum); }, /** * 點擊標籤 */ handleClickTab(item) { let { tabId, acitve } = item; if(acitve) return; this.hideMore(); this.$store.dispatch('navTab/changeTab', { tabId }); }, /** * 點擊更多 */ handleClickMore() { this.moreStatus = !this.moreStatus; }, /** * 更多標籤點擊 */ handleClickMoreTab(index) { this.hideMore(); this.$store.dispatch('navTab/handleMoreTab', { index }); }, /** * 關閉標籤 */ handleCloseTab(index) { this.$store.dispatch('navTab/deleteTab', { index }); }, /** * 關閉所有 */ handleClickDelAll() { if(this.navTabs.length === 1) return; this.hideMore(); this.$store.dispatch('navTab/deleteAllTab'); }, /** * 關閉其餘 */ handleClickDelOther() { if(this.navTabs.length === 1) return; this.hideMore(); this.$store.dispatch('navTab/deleteOtherTab'); }, /** * 隱藏更多列表 */ hideMore() { this.moreStatus = false; } }}</script>複製代碼
建立一個標籤頁面
handleRowClick(row) {
let { id, title, intro } = row;
this.$store.dispatch('navTab/addTab', {
tabId: 'detail_' + id,
tabName: title,
vName: 'detail',
pParams: {
title,
intro
}
})
}複製代碼
/**
* 獲取當前標籤的傳遞參數
* @param {Array} tabs 標籤數據
*/
export const getCurTabParams = (tabs) => {
if(!tabs || !Array.isArray(tabs)) return {};
// 查找當前標籤
let curTab = tabs.filter(item => {
return item.active;
});
return curTab.length > 0 ? curTab[0].pParams : {};
}複製代碼
computed: {
...mapGetters({
navTabs: 'navTab/getNavTabs'
}),
tabParams() {
return getCurTabParams(this.navTabs) ? getCurTabParams(this.navTabs) : {};
}
},複製代碼
附上項目地址:github.com/GuJiBao/vue…