当前位置:首页 > VUE

vue 用户功能实现

2026-02-20 11:08:11VUE

用户登录功能实现

使用 Vue 和 Vuex 管理用户登录状态,结合后端 API 进行身份验证。

模板部分

<template>
  <div>
    <input v-model="username" placeholder="用户名" />
    <input v-model="password" type="password" placeholder="密码" />
    <button @click="login">登录</button>
    <p v-if="error">{{ error }}</p>
  </div>
</template>

脚本部分

<script>
import { mapActions } from 'vuex';

export default {
  data() {
    return {
      username: '',
      password: '',
      error: '',
    };
  },
  methods: {
    ...mapActions(['loginUser']),
    async login() {
      try {
        await this.loginUser({ username: this.username, password: this.password });
        this.$router.push('/dashboard');
      } catch (err) {
        this.error = '登录失败,请检查用户名或密码';
      }
    },
  },
};
</script>

用户注册功能实现

通过表单提交用户注册信息,调用后端 API 完成注册流程。

模板部分

<template>
  <div>
    <input v-model="form.username" placeholder="用户名" />
    <input v-model="form.email" placeholder="邮箱" />
    <input v-model="form.password" type="password" placeholder="密码" />
    <input v-model="form.confirmPassword" type="password" placeholder="确认密码" />
    <button @click="register">注册</button>
    <p v-if="error">{{ error }}</p>
  </div>
</template>

脚本部分

<script>
import { mapActions } from 'vuex';

export default {
  data() {
    return {
      form: {
        username: '',
        email: '',
        password: '',
        confirmPassword: '',
      },
      error: '',
    };
  },
  methods: {
    ...mapActions(['registerUser']),
    async register() {
      if (this.form.password !== this.form.confirmPassword) {
        this.error = '两次输入的密码不一致';
        return;
      }
      try {
        await this.registerUser(this.form);
        this.$router.push('/login');
      } catch (err) {
        this.error = '注册失败,请检查输入信息';
      }
    },
  },
};
</script>

用户信息管理

使用 Vuex 存储用户信息,通过 API 获取或更新用户数据。

Vuex Store 配置

const store = new Vuex.Store({
  state: {
    user: null,
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user;
    },
  },
  actions: {
    async fetchUser({ commit }) {
      const response = await axios.get('/api/user');
      commit('SET_USER', response.data);
    },
    async updateUser({ commit }, userData) {
      const response = await axios.put('/api/user', userData);
      commit('SET_USER', response.data);
    },
  },
});

组件中调用用户信息

<script>
import { mapState, mapActions } from 'vuex';

export default {
  computed: {
    ...mapState(['user']),
  },
  created() {
    this.fetchUser();
  },
  methods: {
    ...mapActions(['fetchUser', 'updateUser']),
    async saveProfile() {
      await this.updateUser(this.user);
    },
  },
};
</script>

用户权限控制

通过路由守卫实现页面访问权限控制,未登录用户重定向到登录页。

路由配置

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

router.beforeEach((to, from, next) => {
  const isAuthenticated = store.state.user !== null;
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

用户注销功能

清除 Vuex 中的用户状态并重定向到登录页。

注销方法实现

vue 用户功能实现

methods: {
  logout() {
    this.$store.commit('SET_USER', null);
    this.$router.push('/login');
  },
}

标签: 功能用户
分享给朋友:

相关文章

h5能实现的功能

h5能实现的功能

H5能实现的功能 H5(HTML5)是一种现代的网页技术标准,具备丰富的功能和特性,适用于多种场景。以下是H5能够实现的主要功能: 多媒体支持 H5原生支持音频和视频播放,无需依赖Flash等插件。…

vue实现注册功能

vue实现注册功能

前端实现注册功能 在Vue中实现注册功能通常需要结合表单验证、HTTP请求和状态管理。以下是一个基于Vue 3和Element Plus的完整实现方案: 模板部分 <template>…

vue实现复选功能

vue实现复选功能

Vue 实现复选功能 基础实现 使用 v-model 绑定到数组实现多选功能。当复选框被选中时,其 value 会被添加到数组中;取消选中时,会从数组中移除。 <template>…

vue实现退出功能

vue实现退出功能

Vue 实现退出功能的方法 在 Vue 中实现退出功能通常涉及清除用户登录状态、跳转登录页等操作。以下是几种常见的实现方式: 清除登录状态并跳转 使用 Vue Router 进行页面跳转,同时清除本…

Vue 实现登出功能

Vue 实现登出功能

实现登出功能的方法 在Vue中实现登出功能通常涉及清除用户认证信息、重置应用状态并跳转到登录页面。以下是几种常见的实现方式: 清除本地存储的token localStorage.removeIte…

vue实现销假功能

vue实现销假功能

实现销假功能的Vue方案 在Vue中实现销假功能通常需要结合前端界面、状态管理和后端API交互。以下是完整的实现方案: 基础组件结构 创建请假表单组件LeaveForm.vue: <temp…