vue3 element-plus-demo 登录认证多页面路由demo

vue3 element-plus-demo 登录认证多页面路由demo

一:效果演示

动画演示

二:设计

a. 目录结构

src
├── App.vue
├── assets
│   ├── element-plus-logo.svg
│   └── logo.png
├── components
├── main.js
├── router
│   └── index.js
├── store
│   └── index.js
└── views
    ├── About.vue
    ├── Home.vue
    ├── Layout.vue			# 模板基本布局,分Header,main 和 footer
    ├── Login.vue			# 登录界面,
    ├── Page1.vue
    └── Page2.vue

b. 页面

页面都继承自Layout.vue

主页页面

Layout.vue

<template>
    <el-container style="height: 100%;">
        <el-header>
            <el-menu :default-active="activeIndex" mode="horizontal" router>
                <el-menu-item>
                    <img src="../assets/element-plus-logo.svg" style="width: 120px; height: 60px;" />
                </el-menu-item>
                <template v-if="username !== ''">
                    <template v-for="route in routes" :key="route.name" :route="route">
                        <el-menu-item :index="route.path">{{ route.meta.displayName }}</el-menu-item>
                    </template>
                    <el-sub-menu style="margin-left: auto;">
                        <template #title>
                            <i class="el-icon-setting"></i>
                            {{ username }}
                        </template>
                        <el-menu-item @click="logoff">
                            <i class="el-icon-right"></i>
                            注销登陆
                        </el-menu-item>
                    </el-sub-menu>
                </template>
                <template v-else>
                    <el-menu-item class="is-active">请登陆后访问!</el-menu-item>
                </template>
            </el-menu>
        </el-header>
        <el-main>
            <slot name="main"></slot>
        </el-main>
        <el-footer height="40px" style="color: #909399; text-align: center;">©2021 Leo xxxxxx.com</el-footer>
    </el-container>
</template>

<script>

import { useRoute, useRouter } from "vue-router";
import { ElMessage } from 'element-plus'

export default {
    name: 'Layout',
    setup() {
        const router = useRouter();
        const route = useRoute();
        return {
            routes: router.getRoutes().filter(value => value.name != 'Login' && value.name != 'Logout'),
            activeIndex: route.path,
        }
    },
    computed: {
        username() {
            return this.$store.state.username || ''
        }
    },
    methods: {
        logoff() {
            this.$store.commit('logoff')
            ElMessage({
                showClose: true,
                message: `用户 ${this.$store.state.username} 注销成功!`,
                type: 'success',
            })
            this.$router.replace({ name: 'Login' })
        }
    }
}
</script>

Home.vue

<template>
<Layout>
  <template v-slot:main>
    <h1>Main Content</h1>
  </template>

</Layout>
</template>

<script>
// @ is an alias to /src

import Layout from "../views/Layout.vue";

export default {
  name: 'Home',
  components: {
    Layout
  }
}
</script>

c. 路由

登录处理, Login.vue

<template>
    <Layout>
        <template v-slot:main>
            <div style="display: flex; justify-content: center; align-items: center; height: 100%;">
                <el-form
                    ref="loginForm"
                    :model="loginForm"
                    status-icon
                    :rules="rules"
                    label-width="120px"
                    style="width: 400px;"
                >
                    <el-form-item label="用户" prop="username" required>
                        <el-input v-model="loginForm.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="userpwd" required>
                        <el-input
                            v-model="loginForm.userpwd"
                            type="password"
                            autocomplete="off"
                            show-password
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            type="primary"
                            @click="submitForm('loginForm')"
                            style="width: 100%;"
                        >登陆</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </template>
    </Layout>
</template>
<script>

import { ElMessage } from 'element-plus'
import Layout from '../views/Layout.vue';


export default {
    data() {
        return {
            loginForm: {
                username: '',
                userpwd: ''
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                userpwd: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            },
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$store.commit('login', this.loginForm.username)
                    ElMessage({
                        showClose: true,
                        message: `用户 ${this.loginForm.username} 登陆成功!`,
                        type: 'success',
                    })
                    this.$router.replace({ name: 'Home' })
                } else {
                    return false
                }
            })
        }
    },
    components: {
        Layout
    }
}
</script>


三: 完整代码

https://github.com/LeoBest2/LearnLib/tree/main/element-plus/src

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Vue3 Element-Plus 登录页面是一个基于 Vue3 和 Element-Plus 组件库开发的登录页面。它包含了登录表单、表单验证、登录按钮等基本功能,同时还可以根据需要进行自定义配置和样式调整。使用 Vue3 Element-Plus 登录页面可以快速搭建一个美观、功能齐全的登录页面,提高用户体验和安全性。 ### 回答2: Vue3是目前最新的Vue版本,Element Plus是一款基于Vue3的UI库,提供了非常全面的组件库,包括了登录页面的相关组件。在使用Vue3和Element Plus构建登录页面时,我们可以采用下面的步骤: 1.安装Element Plus 使用npm或者yarn安装Element Plus: ```bash npm i element-plus --save ``` 2.组件导入 在Vue页面中导入需要用到的组件,例如Input、Button、Form等。 ```javascript <template> <el-form :model="form" ref="form" label-width="80px"> <el-form-item label="用户名" required> <el-input v-model="form.username" placeholder="请输入用户名"></el-input> </el-form-item> <el-form-item label="密码" required> <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm">登录</el-button> </el-form-item> </el-form> </template> <script> import { ref } from 'vue' import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus' export default { components: { ElForm, ElFormItem, ElInput, ElButton }, setup() { const form = ref({ username: '', password: '' }) const submitForm = () => { // 提交表单 } return { form, submitForm } } } </script> ``` 3.调用API 在调用API时,Element Plus提供了非常丰富的组件API,例如Input组件提供了blur、focus等事件和clear方法,可以方便地对输入框进行控制。 ```javascript <template> <el-input v-model="form.username" placeholder="请输入用户名" @blur="handleBlur"></el-input> </template> <script> import { ref } from 'vue' import { ElInput } from 'element-plus' export default { components: { ElInput }, setup() { const form = ref({ username: '' }) const handleBlur = () => { console.log('失去焦点') } return { form, handleBlur } } } </script> ``` 总之,Vue3和Element Plus的组合可以轻松地构建出美观、易用的登录页面,开发者只需要关注业务逻辑即可。 ### 回答3: Vue3是一种轻量级的JavaScript框架,而Element Plus是一种完整的UI框架。它们可以相互配合使用,快速构建现代化的登录页面。 首先,我们需要使用Vue CLI创建一个基本的Vue3项目。然后,我们可以使用npm安装Element Plus,例如运行命令“npm install element-plus”。 接下来,我们可以使用Element Plus提供的UI组件来呈现我们的登录页面。一个简单的登录页面通常由表单组件包含“用户名”和“密码”两个输入框,以及“登录”按钮组成。 我们可以使用Element Plus的表单组件来创建这些输入框,并通过v-model指令绑定数据。例如,在模板中我们可以这样写: <el-form> <el-form-item label="用户名"> <el-input v-model="username"></el-input> </el-form-item> <el-form-item label="密码"> <el-input v-model="password" type="password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="login">登录</el-button> </el-form-item> </el-form> 这个模板代码中,我们使用了三个组件:el-form、el-input和el-button。每个组件都有一个属性来控制它的外观和行为。例如,我们可以通过设置label属性来为输入框添加标签,通过设置type属性来指定密码框的类型。 我们还可以使用一个方法来处理“登录”按钮的点击事件。登录方法可以使用Vue Router将用户重定向到另一个页面,或者使用axios发送POST请求来发起登录操作。例如,在Vue实例中,我们可以这样添加一个login方法: export default { data() { return { username: '', password: '' } }, methods: { login() { axios .post('/login', { username: this.username, password: this.password }) .then(response => { // 登录成功 }) .catch(error => { // 登录失败 }) } } } 在这个示例中,我们定义了一个Vue组件,并使用data属性来声明存储数据的变量。我们还定义了一个login方法,该方法使用axios库向服务器发送POST请求来登录。如果请求成功,我们将在then语句块中处理登录成功的情况,否则,我们将在catch语句块中处理登录失败的情况。 最后,我们可以使用Vue Router将登录页面和其他页面连接起来。Vue Router可以非常容易地处理路由,并将URL映射到组件中。例如,在Vue实例中,我们可以这样配置Vue Router: const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', component: LoginPage }, { path: '/dashboard', component: DashboardPage } ] }) 在这个示例中,我们使用Vue Router的createRouter函数创建一个路由器实例,并使用createWebHistory函数指定路由模式。我们还定义了两个路由:一个指向登录页面,另一个指向仪表板页面。在组件中,我们可以使用this.$router.push('/dashboard')方法来将用户重定向到仪表板页面。 总之,使用Vue3和Element Plus可以快速创建漂亮和功能强大的登录页面。我们可以使用Element Plus提供的UI组件呈现登录表单,并使用Vue Router和axios轻松处理路由和网络请求。同时,Vue3还提供了很多便捷的特性,例如Composition API和Teleport,可以帮助我们更加高效地编写代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值