Vue脚手架写一个登录成功跳转到其他页面的功能有element的登录标签写要求登录成功或错误有提示
时间: 2024-03-22 09:41:36 浏览: 14
好的,首先你需要安装 Vue 脚手架和 Element UI 组件库。然后在你的登录页面中,使用 Element UI 的表单组件和表单验证规则来实现登录功能。示例代码如下:
```html
<template>
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="80px">
<el-form-item label="用户名" prop="username">
<el-input v-model="loginForm.username" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">登录</el-button>
</el-form-item>
</el-form>
</template>
<script>
import { mapActions } from 'vuex';
export default {
data() {
return {
loginForm: {
username: '',
password: ''
},
loginRules: {
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}
};
},
methods: {
...mapActions(['login']),
onSubmit() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.login(this.loginForm).then(() => {
this.$router.push('/dashboard');
}).catch(error => {
this.$message.error(error.message);
});
} else {
return false;
}
});
}
}
};
</script>
```
在上面的代码中,我们使用了 Element UI 的表单组件 `el-form` 和表单验证规则 `loginRules` 来实现了一个登录表单。当用户点击登录按钮时,我们会先对表单进行验证,如果验证通过,就调用 Vuex 中的 `login` action 来进行登录,然后将用户重定向到 `/dashboard` 页面。如果验证不通过,就会显示相应的错误提示。
需要注意的是,`login` action 的具体实现需要根据你的后端 API 来进行,这里不再赘述。此外,为了能够使用 Vuex 中的 `login` action,你还需要在 Vuex 中定义相应的 `actions` 和 `mutations`。