编程随笔 编程随笔
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 知识点

  • 代码调试

  • vue2

    • vue-element-admin使用
    • el-upload封装
    • 文件上传
    • mavon-editor局部引入
    • vuex使用
  • vue3

  • react

  • typescript

  • 前端
  • vue2
华总
2023-08-25
0
0

vuex使用原创

总体流程

store => mutation => action => 组件内this.$store.dispatch()调用(非async异步)

store

 const getters = {
  token: state => state.user.token,
  userAvatar: state => state.user.userAvatar,
  userProfile: state => state.user.userProfile
}
1
2
3
4
5

mutation

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_PROFILE: (state, userProfile) => {
    state.userProfile = userProfile
  },
  SET_AVATAR: (state, userAvatar) => {
    state.userAvatar = userAvatar
  },
}

1
2
3
4
5
6
7
8
9
10
11
12

action

const action = {
    updateInfo({ commit, state }, data) {
    const { profile, avatar } = data
    return new Promise((resolve, reject) => {
      changeProfile({
        userProfile: profile,
        userAvatar: avatar
      }, state.token).then(res => {
        const { userProfile, userAvatar } = res.data
        if (res.code !== 0) {
          reject('请求参数错误')
        }
        commit('SET_PROFILE', userProfile)
        commit('SET_AVATAR', userAvatar)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

组件内使用

updateUserInfo() {
  this.$store.dispatch('user/updateInfo', this.form).then(() => {
    this.$message({
      message: '修改成功',
      type: 'success',
      duration: 5 * 1000
    })
    this.dialogFormVisible = false
    this.flag = !this.flag
    this.form.profile = ''
  })
}
1
2
3
4
5
6
7
8
9
10
11
12
#前端
上次更新: 2024/01/14 16:27:31
mavon-editor局部引入
注意事项

← mavon-editor局部引入 注意事项→

最近更新
01
element-plus多文件手动上传 原创
11-03
02
TrueLicense 创建及安装证书 原创
10-25
03
手动修改迅捷配置 原创
09-03
04
安装 acme.sh 原创
08-29
05
zabbix部署 原创
08-20
更多文章>
Copyright © 2023-2024 liyao52033
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式