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>
相关推荐
2501_916007473 小时前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_916007471 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
小徐_23331 天前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程
Liu.7741 天前
uni-app 组件 uni-easyinput 常见 Bug 及解决方案
uni-app·bug
小徐_23332 天前
uni-app 项目别再从零搭了!3 个 Wot UI 起手模板怎么选?
前端·uni-app
蜡台2 天前
uniapp pdf文件预览组件
pdf·uni-app·合同·pdfh5
凡泰AI2 天前
如何借助MCP打通企业APP内部服务:从统一调用到小程序承接
小程序·uni-app·app·mpaas·mcp·小程序容器
华玥作者2 天前
uniapp 万条数据不卡顿:我写了个虚拟列表组件 hy-list,原生支持瀑布流
数据结构·uni-app·list·vue3
这是个栗子2 天前
uni-app 微信小程序开发:常用函数总结(一)
微信小程序·小程序·uni-app·getcurrentpages
宠友信息4 天前
消息序号如何保证即时通讯源码聊天记录稳定加载
java·spring boot·redis·python·mysql·uni-app