Vuex之集成

安装 vuex

npm i vuex -S

创建文件夹

创建 client/ store/

创建 client/store/store.js 应用入口

声明 store

// store.js
import Vuex from 'vuex'
import Vue from 'vue'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    updateCount (state, num) {
      state.count = num
    }
  }
})

export default store

store 的引入

// index.js 应用入口(最外层)
import Vuex from 'vuex'
import store from './store/store'  // 引入

Vue.use(Vuex)

const root = document.createElement('div')
document.body.appendChild(root)

new Vue({
  router, 
  store, // 注册
  render: (h) => h(App)
}).$mount(root)

store 的使用

显示 store 数据

// app.vue 根组件
<template>
  <div id="app">
    <p>{{count}}</p> // 显示 0
</template>

<script>
export default {
  mounted () {
    console.log(this.$store)
  },
  computed: {
    count () {
      return this.$store.state.count
    }
  }
}
</script>

组件内部,调用 $store 的 commit 方法,修改 mutation

// app.vue 根组件
<template>
  <div id="app">
    <p>{{count}}</p> // 每秒加 1
</template>
<script>
    mounted () {
        console.log(this.$store)
        let i = 1
        setInterval(() => {
          this.$store.commit('updateCount', i++)
        }, 1000)
      },
      computed: {
        count () {
          return this.$store.state.count
        }
      }
</script>

修改声明的 store

和 vue-router 类似,export 时,需要是一个 function,因为服务端渲染,每次渲染都会新生成一个 store,不能使用同一个 store,使用同一个 store 会有内存溢出的问题。

import Vuex from 'vuex'

export default () => {
  return new Vuex.Store({
    state: {
      count: 0
    },
    mutations: {
      updateCount (state, num) {
        state.count = num
      }
    }
  })
}

对应修改入口文件 store 的引入

import Vue from 'vue'
import Vuex from 'vuex'

import createStore from './store/store'

Vue.use(Vuex)

const store = createStore()

const root = document.createElement('div')
document.body.appendChild(root)

new Vue({
  store,
  render: (h) => h(App)
}).$mount(root)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Uni-app 中使用 Vuex 需要进行以下步骤: 1. 在主包(main package)中安装 Vuex:打开终端,进入主包目录,运行以下命令: ``` npm install vuex ``` 2. 创建 Vuex store:在主包目录下创建一个名为 `store` 的文件夹,在该文件夹下创建 `index.js` 文件,并编写 Vuex 的配置和逻辑。例如: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { // 状态数据 }, mutations: { // 修改状态数据的方法 }, actions: { // 异步操作的方法 }, getters: { // 获取状态数据的方法 } }) export default store ``` 3. 在主包入口文件(如 `main.js`)中引入 Vuex store,并注册到 Vue 实例中: ```javascript import Vue from 'vue' import App from './App.vue' import store from './store' const app = new Vue({ store, render: h => h(App) }) app.$mount() ``` 4. 在需要使用 Vuex 的页面中引入 `mapState`、`mapMutations`、`mapActions` 和 `mapGetters` 辅助函数,以便使用 Vuex 提供的数据和方法。例如: ```vue <template> <div> <p>{{ count }}</p> <button @click="increment">增加</button> </div> </template> <script> import { mapState, mapMutations } from 'vuex' export default { computed: { ...mapState(['count']) }, methods: { ...mapMutations(['increment']) } } </script> ``` 这样,你就可以在 Uni-app 中使用 Vuex 来管理全局的状态数据了。当然,在分包中使用 Vuex 的方法与上述类似,只需要在分包的入口文件中引入 Vuex store,并将其注册到 Vue 实例中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值