当前位置:首页 > VUE

vue用户功能实现

2026-03-10 03:15:00VUE

Vue用户功能实现方法

用户登录功能

使用Vue Router和axios实现用户登录功能。创建登录表单组件,包含用户名和密码输入框。

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

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async handleLogin() {
      try {
        const response = await axios.post('/api/login', {
          username: this.username,
          password: this.password
        })
        localStorage.setItem('token', response.data.token)
        this.$router.push('/dashboard')
      } catch (error) {
        console.error('登录失败', error)
      }
    }
  }
}
</script>

用户注册功能

实现用户注册流程,包括表单验证和API调用。

<template>
  <form @submit.prevent="handleRegister">
    <input v-model="form.username" placeholder="用户名"/>
    <input v-model="form.email" type="email" placeholder="邮箱"/>
    <input v-model="form.password" type="password" placeholder="密码"/>
    <input v-model="form.confirmPassword" type="password" placeholder="确认密码"/>
    <button type="submit">注册</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      }
    }
  },
  methods: {
    validateForm() {
      return this.form.password === this.form.confirmPassword
    },
    async handleRegister() {
      if (!this.validateForm()) {
        alert('两次密码输入不一致')
        return
      }
      try {
        await axios.post('/api/register', this.form)
        this.$router.push('/login')
      } catch (error) {
        console.error('注册失败', error)
      }
    }
  }
}
</script>

用户权限管理

使用Vuex实现全局用户状态管理和权限控制。

// store/user.js
export default {
  state: {
    user: null,
    isAuthenticated: false
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user
      state.isAuthenticated = !!user
    }
  },
  actions: {
    async fetchUser({ commit }) {
      try {
        const response = await axios.get('/api/user')
        commit('SET_USER', response.data)
      } catch (error) {
        commit('SET_USER', null)
      }
    }
  }
}

用户信息展示

创建用户个人中心页面,展示用户信息和操作选项。

<template>
  <div v-if="user">
    <h3>{{ user.username }}</h3>
    <p>邮箱: {{ user.email }}</p>
    <p>注册时间: {{ formatDate(user.createdAt) }}</p>
    <button @click="logout">退出登录</button>
  </div>
</template>

<script>
export default {
  computed: {
    user() {
      return this.$store.state.user.user
    }
  },
  methods: {
    formatDate(date) {
      return new Date(date).toLocaleString()
    },
    logout() {
      localStorage.removeItem('token')
      this.$store.commit('SET_USER', null)
      this.$router.push('/login')
    }
  },
  created() {
    this.$store.dispatch('fetchUser')
  }
}
</script>

用户列表管理

实现管理员查看用户列表功能,包含分页和搜索。

vue用户功能实现

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索用户"/>
    <table>
      <tr v-for="user in filteredUsers" :key="user.id">
        <td>{{ user.username }}</td>
        <td>{{ user.email }}</td>
        <td>{{ user.role }}</td>
      </tr>
    </table>
    <div class="pagination">
      <button @click="prevPage">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      users: [],
      searchQuery: '',
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    filteredUsers() {
      return this.users.filter(user => 
        user.username.includes(this.searchQuery) || 
        user.email.includes(this.searchQuery)
      ).slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      )
    }
  },
  methods: {
    async fetchUsers() {
      try {
        const response = await axios.get('/api/users')
        this.users = response.data
      } catch (error) {
        console.error('获取用户列表失败', error)
      }
    },
    nextPage() {
      this.currentPage++
    },
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    }
  },
  created() {
    this.fetchUsers()
  }
}
</script>

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

相关文章

vue实现复选功能

vue实现复选功能

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

vue实现修改功能

vue实现修改功能

Vue 实现修改功能的方法 数据绑定与表单处理 使用v-model实现双向数据绑定,快速获取表单输入值。适用于表单类修改场景: <template> <input v-mode…

php实现登录功能

php实现登录功能

创建登录表单 在HTML中创建一个表单,包含用户名和密码输入框。表单的action属性指向处理登录的PHP文件,method设置为POST。 <form action="login.php"…

vue 实现备注功能

vue 实现备注功能

Vue 实现备注功能的方法 数据绑定与基础表单 在 Vue 中实现备注功能,可以通过 v-model 实现双向数据绑定。创建一个文本输入框或文本域,绑定到组件的 data 属性: <templ…

vue实现预览功能

vue实现预览功能

Vue 实现文件预览功能 使用 FileReader 实现本地文件预览 通过 HTML5 的 FileReader API 可以读取本地文件内容并显示预览。创建一个文件上传组件,监听 change…

vue实现目录功能

vue实现目录功能

Vue实现目录功能的方法 基于路由的目录结构 在Vue项目中,可以通过路由配置自动生成目录结构。使用vue-router的routes数组可以定义页面层级关系,结合递归组件渲染目录树。 // rou…