当前位置:首页 > VUE

vue实现登了页面

2026-02-18 20:16:11VUE

Vue 实现登录页面

使用 Vue 实现登录页面通常需要结合 Vue 的核心功能(如数据绑定、事件处理)和 Vue Router 进行页面跳转。以下是一个完整的实现方案:

1. 创建 Vue 项目

确保已安装 Vue CLI,通过以下命令创建项目:

vue create vue-login-demo

2. 安装 Vue Router

在项目目录中运行:

npm install vue-router

3. 项目结构

典型结构如下:

src/
├── components/
│   └── Login.vue
├── router/
│   └── index.js
├── App.vue
└── main.js

4. 实现登录组件 (Login.vue)

<template>
  <div class="login-container">
    <h2>登录</h2>
    <form @submit.prevent="handleSubmit">
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          type="text" 
          id="username" 
          v-model="username" 
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          id="password" 
          v-model="password" 
          required
        />
      </div>
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleSubmit() {
      // 模拟登录逻辑
      if (this.username && this.password) {
        localStorage.setItem('isAuthenticated', 'true')
        this.$router.push('/dashboard')
      } else {
        alert('请输入用户名和密码')
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 15px;
}
</style>

5. 配置路由 (router/index.js)

import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Dashboard from '../components/Dashboard.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: Dashboard,
      meta: { requiresAuth: true }
    },
    {
      path: '*',
      redirect: '/login'
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('isAuthenticated')
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router

6. 修改主文件 (main.js)

import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

7. 修改 App.vue

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

8. 创建 Dashboard 组件

<template>
  <div>
    <h2>欢迎来到仪表盘</h2>
    <button @click="handleLogout">退出登录</button>
  </div>
</template>

<script>
export default {
  methods: {
    handleLogout() {
      localStorage.removeItem('isAuthenticated')
      this.$router.push('/login')
    }
  }
}
</script>

9. 运行项目

npm run serve

10. 扩展功能建议

  • 添加表单验证(可使用 Vuelidate 或 VeeValidate)
  • 集成真实的 API 调用(使用 axios)
  • 添加加载状态和错误处理
  • 实现记住我功能
  • 添加第三方登录(如 Google、GitHub)

这个实现包含了基本的登录功能、路由保护和状态管理(使用 localStorage)。根据实际需求,可以进一步扩展和完善功能。

vue实现登了页面

标签: 页面vue
分享给朋友:

相关文章

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…