当前位置:首页 > VUE

vue-cli实现登录

2026-01-21 04:32:42VUE

vue-cli 实现登录功能

创建登录组件

src/components 目录下创建 Login.vue 文件,包含表单和基础样式:

<template>
  <div class="login-container">
    <form @submit.prevent="handleSubmit">
      <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: {
    handleSubmit() {
      this.$emit('login', {
        username: this.username,
        password: this.password
      })
    }
  }
}
</script>

配置路由

src/router/index.js 中添加登录路由:

import Login from '@/components/Login'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  }
]

实现认证逻辑

创建 src/services/auth.js 处理认证逻辑:

export default {
  login(credentials) {
    return axios.post('/api/login', credentials)
  },
  logout() {
    return axios.post('/api/logout')
  }
}

状态管理

在 Vuex store (src/store/index.js) 中管理登录状态:

const store = new Vuex.Store({
  state: {
    isAuthenticated: false,
    user: null
  },
  mutations: {
    SET_AUTH(state, payload) {
      state.isAuthenticated = payload.isAuthenticated
      state.user = payload.user
    }
  },
  actions: {
    async login({ commit }, credentials) {
      try {
        const response = await authService.login(credentials)
        commit('SET_AUTH', {
          isAuthenticated: true,
          user: response.data.user
        })
        router.push('/dashboard')
      } catch (error) {
        console.error('Login failed:', error)
      }
    }
  }
})

路由守卫

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

router.beforeEach((to, from, next) => {
  const isAuthenticated = store.state.isAuthenticated
  if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' })
  } else {
    next()
  }
})

表单验证

为登录表单添加基础验证:

<template>
  <form @submit.prevent="handleSubmit">
    <div v-if="errors.length">
      <p v-for="error in errors" :key="error">{{ error }}</p>
    </div>
    <!-- 表单内容 -->
  </form>
</template>

<script>
export default {
  data() {
    return {
      errors: []
    }
  },
  methods: {
    validateForm() {
      this.errors = []
      if (!this.username) this.errors.push('用户名必填')
      if (!this.password) this.errors.push('密码必填')
      return this.errors.length === 0
    },
    handleSubmit() {
      if (this.validateForm()) {
        this.$emit('login', {
          username: this.username,
          password: this.password
        })
      }
    }
  }
}
</script>

样式优化

添加基础样式增强用户体验:

.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}

input {
  display: block;
  width: 100%;
  margin-bottom: 10px;
  padding: 8px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
}

vue-cli实现登录

标签: vuecli
分享给朋友:

相关文章

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <b…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…