我来帮你设计一个基于 Vue 2 的 SPA 系统架构方案。我会按照需求逐步分析并提供具体实现代码。
首先是基础的 HTML 结构:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue 2 SPA System</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-router@3.5.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vuex@3.6.2"></script>
<style>
.active-menu { color: #409EFF; }
.nav-tab { display: inline-block; padding: 8px 16px; }
.nav-tab.active { background: #409EFF; color: white; }
</style>
</head>
<body>
<div id="app">
<template v-if="!isAuthenticated">
<login-view></login-view>
</template>
<template v-else>
<div class="layout">
<div class="sidebar">
<side-menu :menus="menus"></side-menu>
</div>
<div class="main">
<nav-tabs></nav-tabs>
<router-view></router-view>
</div>
</div>
</template>
</div>
</body>
</html>
下面是核心功能实现:
- Vuex 状态管理:
const store = new Vuex.Store({
state: {
user: null,
menus: [],
tabs: [],
activeTab: null
},
mutations: {
setUser(state, user) {
state.user = user;
},
setMenus(state, menus) {
state.menus = menus;
},
addTab(state, tab) {
if (!state.tabs.find(t => t.path === tab.path)) {
state.tabs.push(tab);
}
state.activeTab = tab.path;
},
removeTab(state, path) {
const index = state.tabs.findIndex(tab => tab.path === path);
state.tabs.splice(index, 1);
},
setActiveTab(state, path) {
state.activeTab = path;
}
}
});
- 路由配置:
const router = new VueRouter({
routes: [
{
path: '/login',
component: LoginView
},
{
path: '/',
component: Layout,
children: [] // 动态添加路由
}
]
});
// 路由守卫
router.beforeEach((to, from, next) => {
if (!store.state.user && to.path !== '/login') {
next('/login');
} else {
next();
}
});
- 登录组件:
Vue.component('login-view', {
template: `
<div class="login">
<input v-model="username" placeholder="用户名">
<input v-model="password" type="password" placeholder="密码">
<button @click="handleLogin">登录</button>
</div>
`,
data() {
return {
username: '',
password: ''
}
},
methods: {
async handleLogin() {
// 模拟登录请求
const response = await mockLogin(this.username, this.password);
if (response.success) {
this.$store.commit('setUser', response.user);
this.$store.commit('setMenus', response.menus);
this.generateRoutes(response.menus);
this.$router.push('/');
}
},
generateRoutes(menus) {
const routes = this.convertMenuToRoutes(menus);
router.addRoutes(routes);
}
}
});
- 侧边栏菜单组件:
Vue.component('side-menu', {
props: ['menus'],
template: `
<div class="menu">
<template v-for="menu in menus">
<div v-if="!menu.children"
:key="menu.path"
:class="{'active-menu': $route.path === menu.path}"
@click="navigate(menu)">
{{menu.title}}
</div>
<div v-else :key="menu.path">
<div>{{menu.title}}</div>
<side-menu :menus="menu.children"></side-menu>
</div>
</template>
</div>
`,
methods: {
navigate(menu) {
this.$router.push(menu.path);
this.$store.commit('addTab', {
title: menu.title,
path: menu.path
});
}
}
});
- 标签导航组件:
Vue.component('nav-tabs', {
template: `
<div class="tabs">
<div v-for="tab in tabs"
:key="tab.path"
:class="['nav-tab', {'active': activeTab === tab.path}]"
@click="switchTab(tab)">
{{tab.title}}
<span @click.stop="closeTab(tab)">×</span>
</div>
</div>
`,
computed: {
tabs() {
return this.$store.state.tabs;
},
activeTab() {
return this.$store.state.activeTab;
}
},
methods: {
switchTab(tab) {
this.$router.push(tab.path);
this.$store.commit('setActiveTab', tab.path);
},
closeTab(tab) {
this.$store.commit('removeTab', tab.path);
if (this.activeTab === tab.path) {
const lastTab = this.tabs[this.tabs.length - 1];
if (lastTab) {
this.switchTab(lastTab);
}
}
}
}
});
- 工具函数:
// 模拟登录接口
function mockLogin(username, password) {
return new Promise(resolve => {
setTimeout(() => {
resolve({
success: true,
user: { username },
menus: [
{
title: '首页',
path: '/home'
},
{
title: '用户管理',
path: '/users',
children: [
{
title: '用户列表',
path: '/users/list'
}
]
}
]
});
}, 1000);
});
}
// 菜单转路由配置
function convertMenuToRoutes(menus) {
return menus.map(menu => ({
path: menu.path,
component: () => import(`./views${menu.path}.vue`),
children: menu.children ? convertMenuToRoutes(menu.children) : null
}));
}
这个方案实现了:
- 基于 Token 的身份认证
- 动态菜单生成和路由匹配
- 多标签页导航
- 状态持久化
- 路由参数处理
建议在实际使用时:
- 添加错误处理机制
- 实现数据持久化
- 优化路由加载性能
- 添加必要的动画效果
- 完善页面缓存机制
需要补充或者调整的部分我很乐意继续讨论。