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

样式优化

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

vue-cli实现登录

.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;
}

标签: vuecli
分享给朋友:

相关文章

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

vue实现甘特图

vue实现甘特图

使用 Vue 实现甘特图 基于开源库(如 vue-ganttastic) 安装依赖库: npm install vue-ganttastic 示例代码: <template> &l…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…

用vue实现echarts

用vue实现echarts

使用 Vue 实现 ECharts 安装依赖 在 Vue 项目中安装 ECharts 和 Vue-ECharts(官方推荐的 Vue 封装库): npm install echarts vue-ec…