Vuex总结

vuex  官网: ‘专为 Vue.js 应用程序开发的状态管理模式’。多个视图引用/修改同一状态时,依靠组件间数据的传递很麻烦,vuex将这些公用的状态数据统一进行管理。

vuex的应用场景:想用的时候用,一般中大型项目。

当vue组件从store中读取状态时,若store中的状态发生变化,相应的引入此状态的组件也会相应的的高效更新。改变store状态(state)中唯一的方法就是通过mutation commit数据。

 state:  视图公用的数据,(被多个视图引用、修改)

 getters: 公用数据不符合你想要的格式,在这里加工处理。

 mutations:  提供更改数据的方法  store.commit()    mutations的属性方法都必须是同步的

 actions:    更改公用数据的方法,但是actions更改数据仍然需要通过mutations 中store.commit()更改数据。都是更改数据mutations就好了,何必再多actions这一步呢。actions存在的意义是: 异步处理,在actions中进行异步处理,再同步提交给mutations,同步更改时间。

 modules :modules可以分出多个store模块。每个模块拥有自己的state、getters、mutations、actions.

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

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    banner: 'https://w.wallhaven.cc/full/28/wallhaven-281d5y.png',
    themes: [
      {
        themeId: '000001',
        themeName: '咒术回战',
        themeBanner: 'https://images7.alphacoders.com/114/thumb-1920-1144724.png'
      },
      {
        themeId: '000002',
        themeName: '一拳超人',
        themeBanner: 'https://images3.alphacoders.com/104/thumb-1920-1046030.jpg'
      },
      {
        themeId: '000003',
        themeName: '火影忍者',
        themeBanner: 'https://images5.alphacoders.com/532/thumb-1920-532559.jpg'
      },
      {
        themeId: '000004',
        themeName: '鬼灭之刃',
        themeBanner: 'https://images3.alphacoders.com/105/thumb-1920-1053268.jpg'
      }]
  },

  getters: {
    themesNew(state) {
      state.themes.map(item=>{
        item.themeName += item.themeId
        return item
      })
      return state.themes
    }
  },

  mutations: {
    changeBanner(state, value){
      state.banner = value
    }
  },

  actions: {
    changeBannerActions({commit}, value) {
      // 模仿异步操作
      setTimeout(()=> {
        // 提交到mutations, 让mutations更新数据
        commit('changeBanner', value)
      }, 3000)
    }
  }

})

export default store;

main.js 全局引用就不写了。

<template>
  <div class="site-page">
    <div class="banner" :style="{backgroundImage: 'url(' + banner +')'}">
    </div>
    <div class="left-side">
      <p class="site-name">Anime Theme</p>
      <ul>
        <li v-for="item in themesNew" @click="changeBanner" :data-themebanner="item.themeBanner">{{ item.themeName }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
  import store from "@/store/store";

  export default {
    name: 'login',
    data() {
      return {

      }
    },
    computed: {
      // 获取state数据
      banner() {
        return this.$store.state.banner
      },
      // 获取getters数据
      themesNew() {
        return this.$store.getters.themesNew
      }
    },
    methods: {
      changeBanner(e) {
        const { themebanner } = e.currentTarget.dataset
        // 调用actions,提交数据
        this.$store.dispatch('changeBannerActions', themebanner)
      }
    }
  }
</script>

<style lang="less">
  .site-page {
    .banner {
      width: 100vw;
      height: 100vh;
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
    }
    .left-side {
      position: fixed;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      width: 240px;
      background: rgba(0,0,0,.5);
      z-index: 1;

      .site-name {
        font-family: Futura;
        margin-top: 108px;
        font-size: 28px;
        font-weight: 600;
        color: #fff;
        letter-spacing: 2px;
        user-select: none;
      }

      ul {
        /*border: 1px solid #fff;*/
        margin: 36px auto;
        width: 100%;
        list-style: none;
        li {
          /*border-bottom: 1px solid darkslateblue;*/
          margin: 5px auto;
          width: 80%;
          height: 48px;
          font-size: 18px;
          line-height: 48px;
          color: #fff;
          user-select: none;
          cursor: pointer;
          letter-spacing: 1px;
        }

        li:hover {
          transform: scale(1.2);
          transition: transform .5s;
        }
      }
    }
  }

</style>

通过辅助函数也可以简写: 

<template>
  <div class="site-page">
    <div class="banner" :style="{backgroundImage: 'url(' + banner +')'}">
    </div>
    <div class="left-side">
      <p class="site-name">Anime Theme</p>
      <ul>
        <!--  这里注意actions的参数,直接写括号里传参  -->
        <li v-for="item in themesNew" @click="changeBannerActions(item.themeBanner)" :data-themebanner="item.themeBanner">{{ item.themeName }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
  import { mapState, mapGetters, mapActions} from 'vuex'
  export default {
    name: 'login',
    data() {
      return {

      }
    },
    computed: {
      /* ...扩展运算符,展开mapState和mapGetters对象作为属性放入computed对象中 */
      ...mapState(['banner']),
      ...mapGetters(['themesNew'])
      // banner() {
      //   return this.$store.state.banner
      // },
      // themesNew() {
      //   return this.$store.getters.themesNew
      // }
    },
    methods: {
      ...mapActions(['changeBannerActions'])
      // changeBanner(e) {
      //   const { themebanner } = e.currentTarget.dataset
      //
      //   this.$store.dispatch('changeBannerActions', themebanner)
      // }
    }
  }
</script>

<style lang="less">
  .site-page {
    .banner {
      width: 100vw;
      height: 100vh;
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
    }
    .left-side {
      position: fixed;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      width: 240px;
      background: rgba(0,0,0,.5);
      z-index: 1;

      .site-name {
        font-family: Futura;
        margin-top: 108px;
        font-size: 28px;
        font-weight: 600;
        color: #fff;
        letter-spacing: 2px;
        user-select: none;
      }

      ul {
        /*border: 1px solid #fff;*/
        margin: 36px auto;
        width: 100%;
        list-style: none;
        li {
          /*border-bottom: 1px solid darkslateblue;*/
          margin: 5px auto;
          width: 80%;
          height: 48px;
          font-size: 18px;
          line-height: 48px;
          color: #fff;
          user-select: none;
          cursor: pointer;
          letter-spacing: 1px;
        }

        li:hover {
          transform: scale(1.2);
          transition: transform .5s;
        }
      }
    }
  }




</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值