当前位置:首页 > 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)

测试运行

启动开发服务器:

npm run serve

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

vue-cli实现登录

标签: vuecli
分享给朋友:

相关文章

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue实现slidedown

vue实现slidedown

Vue 实现 SlideDown 动画效果 在 Vue 中实现 SlideDown 效果可以通过 CSS 过渡或动画结合 Vue 的过渡系统完成。以下是几种常见方法: 使用 Vue Transit…