一、this.$store.commit()

直接派发一个事件给mutations

  1. <h2 @click="handleClick">{{this.$store.state.city}}</h2>
  1. <script>
  2. export default {
  3. name: 'home',
  4. methods:{
  5. handleClick(){
  6. /* 派发一个事件 */
  7. this.$store.commit("changeCity")
  8. }
  9. }
  10. }
  11. </script>
  1. mutations: {
  2. changeCity(state){
  3. state.city="成都"
  4. }
  5. },

二、mapState

如果有多个数据需要获取,使用这个辅助函数效率更高

  1. export default new Vuex.Store({
  2. state: {
  3. city:"武汉",
  4. name:"杨丽蓉",
  5. age:21,
  6. sex:'女'
  7. }
  8. ...
  9. })
  1. <template>
  2. <div class="home">
  3. <h2>{{city}}</h2>
  4. <h2>{{age}}</h2>
  5. </div>
  6. </template>
  7. <script>
  8. import {mapState} from 'vuex';
  9. export default {
  10. name: 'home',
  11. computed:{
  12. ...mapState(['city','name','age','sex'])
  13. }
  14. }
  15. </script>

三、mapActions

映射actions中的事件

  1. <template>
  2. <div class="home">
  3. <h2>{{this.$store.state.count}}</h2>
  4. <button @click="increase">增加</button>
  5. <button @click="decrease">减少</button>
  6. </div>
  7. </template>
  8. <script>
  9. import {mapActions} from 'vuex';
  10. export default {
  11. name: 'home',
  12. methods:{
  13. ...mapActions(['increase','decrease'])
  14. }
  15. }
  16. </script>
  1. export default new Vuex.Store({
  2. state: {
  3. count:0
  4. },
  5. mutations: {
  6. myincrease(state){
  7. state.count++;
  8. },
  9. mydecrease(state){
  10. state.count--;
  11. }
  12. },
  13. actions: {
  14. increase(ctx){
  15. ctx.commit("myincrease")
  16. },
  17. decrease(ctx){
  18. ctx.commit("mydecrease")
  19. }
  20. }
  21. ...
  22. })

四、mapMutations

映射mutations中的事件

  1. <template>
  2. <div class="home">
  3. <h2>{{this.$store.state.count}}</h2>
  4. <button @click="myincrease">增加</button>
  5. <button @click="mydecrease">减少</button>
  6. </div>
  7. </template>
  8. <script>
  9. import {mapMutations} from 'vuex';
  10. export default {
  11. name: 'home',
  12. methods:{
  13. ...mapMutations(['myincrease','mydecrease']),
  14. }
  15. }
  16. </script>
  1. export default new Vuex.Store({
  2. state: {
  3. count:0
  4. },
  5. mutations: {
  6. myincrease(state){
  7. state.count++;
  8. },
  9. mydecrease(state){
  10. state.count--;
  11. }
  12. },
  13. actions: {
  14. increase(ctx){
  15. ctx.commit("myincrease")
  16. },
  17. decrease(ctx){
  18. ctx.commit("mydecrease")
  19. }
  20. }
  21. ...
  22. })

五、getters、mapGetters

mapGetters将store中的getters属性映射到局部计算属性
image.png

  1. <template>
  2. <div class="home">
  3. <h2>{{this.$store.state.count}}</h2>
  4. <h3>{{this.$store.getters.myCount}}</h3>
  5. <button @click="increase">增加</button>
  6. <button @click="decrease">减少</button>
  7. </div>
  8. </template>
  9. <script>
  10. import {mapActions,mapGetters} from 'vuex';
  11. export default {
  12. name: 'home',
  13. methods:{
  14. ...mapActions(['increase','decrease'])
  15. },
  16. computed:{
  17. ...mapGetters(['myCount'])
  18. }
  19. }
  20. </script>
  1. export default new Vuex.Store({
  2. ...
  3. getters:{
  4. myCount(state){
  5. return "当前的数量:"+state.count;
  6. }
  7. }
  8. })

六、分模块 modules

作用:当store对象非常复杂的时候,我们可以根据将其拆分成不同的模块

6-1新建modules文件夹,拆分modules

image.png
//user.js

  1. const user = {
  2. state:{
  3. name:"杨丽蓉"
  4. },
  5. mutations:{},
  6. actions:{},
  7. getters:{}
  8. }
  9. export default user;

//info.js

  1. const info = {
  2. state:{
  3. tips:12,
  4. news:15
  5. },
  6. mutations:{
  7. add(state){
  8. state.tips++;
  9. },
  10. reduce(state){
  11. state.tips--;
  12. }
  13. },
  14. actions:{},
  15. getters:{}
  16. }
  17. export default info;

6-2使用数据

  1. <template>
  2. <div class="home">
  3. <h2>首页</h2>
  4. <h3>{{tips}}</h3>
  5. <h4>{{news}}</h4>
  6. <h5>{{name}}</h5>
  7. <button @click="add">增加</button>
  8. <button @click="reduce">减少</button>
  9. </div>
  10. </template>
  11. <script>
  12. import {mapState,mapMutations} from 'vuex'
  13. export default {
  14. name: 'home',
  15. computed:{
  16. ...mapState({
  17. tips:state => state.info.tips,
  18. news:state => state.info.news,
  19. name:state => state.user.name
  20. })
  21. },
  22. methods:{
  23. ...mapMutations(['add','reduce'])
  24. }
  25. }
  26. </script>

七、vuex,axios拦截器实现Loading

7-1设置axios的拦截器

`yarn add axios``
配置axios —-main.js

  1. import axios from 'axios'
  2. Vue.prototype.axios = axios;
  3. axios.interceptors.request.use(function (config) {
  4. // Do something before request is sent
  5. store.state.isLoading = true;
  6. return config;
  7. });
  8. axios.interceptors.response.use(function (response) {
  9. // Do something with response data
  10. store.state.isLoading = false;
  11. return response;
  12. });

7-2在vuex的state属性中设置isLoading

  1. export default new Vuex.Store({
  2. state:{
  3. isLoading:true
  4. }
  5. ...
  6. })

7-3使用

  1. <p v-if="this.$store.state.isLoading">正在加载中</p>

八、模块化导入方案

8-1export default

  1. var a = 10;
  2. var b = 20;
  3. export default {
  4. a,
  5. b
  6. }
  1. //About.vue
  2. import data from './data.js'
  3. mounted(){
  4. console.log(data) //{a:10,b:20}
  5. }

8-2、export

  1. 导出: export {a,b}
  2. 导入: import {a,b} from './data.js'