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>
相关推荐
河北清兮网络科技1 天前
短剧 APP 产品说明
小程序·uni-app·短剧
宠友信息1 天前
一套基于uniapp+springboot完整社区系统是如何实现的?友猫社区源码级功能解析
java·spring boot·后端·微服务·微信·uni-app
碎像2 天前
掌握uniapp发布微信小程序、App(Android)
微信小程序·小程序·uni-app
stpzhf2 天前
uniapp nvue组件多个text在一行并且高亮其中一些文字
前端·javascript·uni-app
qq_316837752 天前
制作uniapp原生插件 在本地离线打包中测试 集成在云打包中
uni-app
Fate_I_C3 天前
uniappx 鸿蒙运行包制作失败
华为·uni-app·uniapp·harmonyos
chQHk57BN3 天前
跨平台前端开发:用Flutter和UniApp一次编写多端运行
flutter·uni-app
自然 醒3 天前
uni-app开发微信小程序,如何使用towxml去渲染md格式和html标签格式的内容?
微信小程序·uni-app·html
CHB3 天前
uni-agent,你的数字员工来了
人工智能·uni-app·vibecoding
h_jQuery3 天前
uniapp使用canvas实现逐字书写任意文字内容,后合成一张图片提交
前端·javascript·uni-app