zoukankan      html  css  js  c++  java
  • uniapp中vuex的基本使用

    1. 创建一个uniapp项目

    2. 在项目目录下用npm安装 vuex

    npm install vuex --save
    

    3. 在项目根目录下创建 store文件夹,在store文件夹中创建 index.js

     4. 在index.js中显式地通过 Vue.use() 来安装 Vuex:

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)

    5.在index.js中创建store

    5.1 完整的store目录如下:

    const store = new Vuex.Store({
      state: {
        // 存放状态
      },
      getters: {
        // state的计算属性
      },
      mutations: {
        // 更改state中状态的逻辑,同步操作
      },
      actions: {
        // 提交mutation,异步操作
      },
      // 如果将store分成一个个的模块的话,则需要用到modules。
       //然后在每一个module中写state, getters, mutations, actions等。
      modules: {
        a: moduleA,
        b: moduleB,
        // ...
      }
    });
    

    5.2 导出store

    export default store
    

    6. 在main.js 中引入store


    vuex的基础用法

    index.js

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    const store = new Vuex.Store({
    	state: {
    		// 存放状态
    		count:0,
    		test:'这是store.js中的数据'
    	},
    	getters: {
    		// state的计算属性
    		//用 this.$store.getters.getData()读取
    		getData(state){
    			return state;
    		}
    	},
    	mutations: {
    		// 更改state中状态的逻辑,同步操作
    		//用 this.$store.commit('function_name',payload) 使用,若无参数则不写payload
    		add(state,n){
    			state.count += n;
    		}
    	},
    	actions: {
    		// 提交mutation,异步操作
    	}
    })
    
    export default store
    

  • 相关阅读:
    要离职了。
    上海找工作经历
    1.6. 三基色LED
    1.5. 板载LED PWM模式
    1.4. 板载LED控制
    1.3. 硬件篇之IO口(视频连接)
    1.2 Hello World
    1.8. 数码管
    ESP32编译自己的micropython固件
    1.1 准备工作
  • 原文地址:https://www.cnblogs.com/sunshine233/p/15339348.html
Copyright © 2011-2022 走看看