uniapp中使用vuex

uniapp中使用vuex

  • 使用前注意
  • vue2语法中使用
    • [1. store/index.js](#1. store/index.js)
    • [2. 在 main.js 中挂载](#2. 在 main.js 中挂载)
    • [3. 在组件中使用](#3. 在组件中使用)
  • vue3语法中使用
    • [1. 创建 store/index.js](#1. 创建 store/index.js)
    • [2. 在 main.js 中挂载](#2. 在 main.js 中挂载)
    • [3. 在组件中使用](#3. 在组件中使用)

使用前注意

  1. uni-app 内置了 Vuex,你不需要通过 npm 安装,直接引入使用即可。
  2. vue2中使用的是Vuex 3,vue3中使用的是Vuex 4,因此使用语法上有些区别。

vue2语法中使用

1. store/index.js

bash 复制代码
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: null,
  },
  mutations: {
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    // 异步操作示例
    async fetchUserInfo({ commit }) {
      const res = await uni.request({ url: 'https://api.example.com/user' })
      commit('SET_USER_INFO', res.data)
    }
  },
  getters: {
    userName: state => state.userInfo?.name || '未登录'
  }
})

2. 在 main.js 中挂载

bash 复制代码
import Vue from 'vue'
import App from './App'
import store from './store'  // 导入刚才创建的 store

Vue.prototype.$store = store  // 可选,便于在组件中通过 this.$store 访问

const app = new Vue({
  store,  // 注入 Vue 实例
  ...App
})
app.$mount()

3. 在组件中使用

bash 复制代码
<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  computed: {
    // 方式一:直接访问 this.$store
    // userName() { return this.$store.getters.userName }

    // 方式二:使用辅助函数(推荐)
    ...mapGetters(['userName'])
  },
  methods: {
    // 方式一:直接调用
    // fetchUserInfo() { this.$store.commit('fetchUserInfo') }

    // 方式二:使用辅助函数
    // ...mapActions(['fetchUserInfo'])   // 异步调用
  }
}
</script>

vue3语法中使用

1. 创建 store/index.js

bash 复制代码
import { createStore } from 'vuex'

const store = createSore({
  state: {
    userInfo: null,
  },
  mutations: {
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo
    },
  },
  actions: {
    async fetchUserInfo({ commit }) {
      const res = await uni.request({ url: 'https://api.example.com/user' })
      commit('SET_USER_INFO', res.data)
    }
  },
  getters: {
    userName: state => state.userInfo?.name || '未登录'
  }
})

export default store

2. 在 main.js 中挂载

bash 复制代码
import App from './App'
import store from './store'
import { createSSRApp } from 'vue'

export function createApp() {
  const app = createSSRApp(App)
  app.use(store)  // 使用 use 挂载
  return { app }
}

3. 在组件中使用

bash 复制代码
<script setup>
import { useStore } from 'vuex'
import { computed } from 'vue'

const store = useStore()
const userName = computed(() => store.getters.userName)
</script>
相关推荐
spmcor20 小时前
身份证读卡“无感登录”方案实践:从手动点击到自动检测
uni-app
PedroQue991 天前
uni-router v1.8.0新增冷启动守卫补执行
前端·uni-app
PedroQue992 天前
uni-router v1.7.0重磅更新:守卫重定向自由掌控
前端·uni-app
一份执念4 天前
uni-app项目 (vue+vite + uni-UI)中引入umd格式JS文件,微信小程序中导入报错处理方案
前端·uni-app·echarts
PedroQue994 天前
V1.6.1性能优化:高频路径提速与代码精简
前端·uni-app
夏碧笔6 天前
uni-app跨端地图实战:用第三方LBS替代微信平台收费服务
uni-app
用户69903048487511 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_11 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
Geek_Vison11 天前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
CHB12 天前
HDC2026 演讲实录|AI 驱动的跨端进化:利用 uni-agent 快速构建高性能鸿蒙应用
uni-app·harmonyos