vue+elementui搭建一个后台管理并实现登录
时间: 2023-08-11 08:07:45 浏览: 80
好的,下面是一个简单的示例,供您参考:
1. 安装 Vue CLI
```
npm install -g @vue/cli
```
2. 创建一个新的 Vue 项目
```
vue create my-project
```
3. 安装 Element UI 和 Axios
```
npm i element-ui axios -S
```
4. 引入 Element UI
在 `main.js` 中添加以下代码:
```js
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
```
5. 创建登录页面
在 `src/views` 目录下创建一个新的文件夹,例如 `Login`,然后在其中创建一个新的 Vue 组件,例如 `index.vue`。
6. 实现登录功能
在 `Login/index.vue` 中添加以下代码:
```html
<template>
<div>
<el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="80px" class="demo-ruleForm">
<el-form-item label="用户名" prop="username">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="form.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">登录</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
form: {
username: '',
password: ''
},
rules: {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' }
]
}
}
},
methods: {
login() {
this.$refs.form.validate(valid => {
if (valid) {
const { username, password } = this.form
// 发送登录请求
this.$axios.post('/api/login', { username, password })
.then(res => {
// 登录成功后的操作
// 例如保存 token 到本地存储中
localStorage.setItem('token', res.data.token)
// 跳转到主页面
this.$router.push('/')
})
.catch(error => {
// 处理登录失败的情况
this.$message.error('登录失败')
})
}
})
}
}
}
</script>
```
7. 创建主页面
在 `src/views` 目录下创建一个新的文件夹,例如 `Dashboard`,然后在其中创建一个新的 Vue 组件,例如 `index.vue`。
8. 添加路由
在 `src/router/index.js` 中添加以下代码:
```js
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login/index.vue'
import Dashboard from '@/views/Dashboard/index.vue'
Vue.use(Router)
const router = new Router({
routes: [
{
path: '/',
name: 'Dashboard',
component: Dashboard,
meta: {
requiresAuth: true
}
},
{
path: '/login',
name: 'Login',
component: Login
}
]
})
// 添加路由守卫
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
// 判断用户是否已登录
const token = localStorage.getItem('token')
if (!token) {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else {
next()
}
} else {
next()
}
})
export default router
```
9. 运行项目
在命令行中运行以下命令:
```
npm run serve
```
以上是一个简单的示例,您可以根据自己的需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)