当前位置:首页 > VUE

vue-cli实现登录

2026-02-21 20:01:55VUE

使用 Vue CLI 实现登录功能

安装 Vue CLI 并创建项目

确保已安装 Node.js,运行以下命令安装 Vue CLI:

npm install -g @vue/cli

创建新项目:

vue create vue-login-demo

选择默认配置或手动配置(推荐包含 Vue Router 和 Vuex)。

配置路由

src/router/index.js 中设置登录路由:

import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'

Vue.use(Router)

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

创建登录页面

src/views/Login.vue 中编写登录表单:

<template>
  <div>
    <h2>登录</h2>
    <form @submit.prevent="handleLogin">
      <input v-model="username" placeholder="用户名" />
      <input v-model="password" type="password" placeholder="密码" />
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleLogin() {
      this.$store.dispatch('login', {
        username: this.username,
        password: this.password
      }).then(() => {
        this.$router.push('/home')
      })
    }
  }
}
</script>

使用 Vuex 管理状态

src/store/index.js 中配置登录逻辑:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isAuthenticated: false,
    user: null
  },
  mutations: {
    SET_AUTH(state, payload) {
      state.isAuthenticated = true
      state.user = payload.user
    }
  },
  actions: {
    login({ commit }, credentials) {
      return new Promise((resolve) => {
        // 模拟 API 调用
        setTimeout(() => {
          commit('SET_AUTH', { user: credentials.username })
          localStorage.setItem('token', 'fake-token')
          resolve()
        }, 500)
      })
    }
  }
})

路由守卫验证

src/router/index.js 中添加全局前置守卫:

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

后端集成(可选)

如需连接真实后端,替换 login action 中的逻辑为实际 API 调用:

actions: {
  async login({ commit }, credentials) {
    const response = await axios.post('/api/login', credentials)
    commit('SET_AUTH', { user: response.data.user })
    localStorage.setItem('token', response.data.token)
  }
}

样式优化

通过 CSS 或 UI 库(如 Element UI、Vuetify)美化登录页面。例如安装 Element UI:

npm install element-ui

main.js 中引入:

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

测试运行

启动开发服务器:

vue-cli实现登录

npm run serve

访问 http://localhost:8080 测试登录流程。

标签: vuecli
分享给朋友:

相关文章

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…