具体功能:前端
在一些后台系统中,见过相似的功能,是利用 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…