用vue2加elementui写一个后台管理系统页面
时间: 2024-05-06 16:19:41 浏览: 126
由于本AI机器人无法进行图形界面的操作,无法提供完整代码,以下是一个简单的示例:
1. 项目初始化
使用Vue CLI创建一个新项目,选择Manually select features并勾选Babel、Router、Vuex、Linter / Formatter和Unit Testing,然后使用npm安装Element UI:
```
vue create admin-system
cd admin-system
vue add element
```
2. 配置路由和菜单
在src/router/index.js中配置路由:
```
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/login',
name: 'Login',
component: Login
},
{
path: '/dashboard',
name: 'Dashboard',
component: Dashboard
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
```
在src/App.vue中配置菜单:
```
<template>
<div id="app">
<el-container>
<el-header>Header</el-header>
<el-container>
<el-aside width="200px">
<el-menu default-active="1" class="el-menu-vertical-demo" @select="handleSelect">
<el-menu-item index="1"><i class="el-icon-menu"></i>Dashboard</el-menu-item>
<el-menu-item index="2"><i class="el-icon-menu"></i>Users</el-menu-item>
<el-submenu index="3">
<template slot="title"><i class="el-icon-menu"></i>Settings</template>
<el-menu-item index="3-1">General</el-menu-item>
<el-menu-item index="3-2">Security</el-menu-item>
</el-submenu>
</el-menu>
</el-aside>
<el-main>
<router-view></router-view>
</el-main>
</el-container>
<el-footer>Footer</el-footer>
</el-container>
</div>
</template>
<script>
export default {
name: 'App',
methods: {
handleSelect(index) {
switch (index) {
case '1':
this.$router.push('/dashboard')
break
case '2':
this.$router.push('/users')
break
case '3-1':
this.$router.push('/settings/general')
break
case '3-2':
this.$router.push('/settings/security')
break
}
}
}
}
</script>
```
3. 编写登录和仪表盘页面
在src/views/Login.vue中编写登录页面:
```
<template>
<div class="login">
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="Username">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="Password">
<el-input type="password" v-model="form.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">Login</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: 'Login',
data() {
return {
form: {
username: '',
password: ''
}
}
},
methods: {
login() {
if (this.form.username === 'admin' && this.form.password === 'admin') {
this.$router.push('/dashboard')
} else {
this.$message.error('Invalid username or password.')
}
}
}
}
</script>
```
在src/views/Dashboard.vue中编写仪表盘页面:
```
<template>
<div class="dashboard">
<h1>Dashboard</h1>
<p>Welcome to the admin system.</p>
</div>
</template>
```
4. 运行项目
在项目根目录下运行npm run serve命令,打开浏览器访问http://localhost:8080即可看到登录页面。输入用户名和密码admin/admin后,将跳转到仪表盘页面。在菜单中选择Settings -> Security可以看到子菜单的显示效果。
阅读全文