• VUEX的基础使用存值及异步


    目录

    什么是VUEX

     有什么作用

     安装

    取值

    异步


    什么是VUEX

    VUEX 是一个用于状态管理的状态容器模式(state management pattern)库,用于 Vue.js 应用程序。它允许你在应用程序中集中管理和共享状态,并提供了一组用于更改状态的规则。VUEX 的核心概念包括 store(仓库)、state(状态)、mutations(变动)和 actions(动作)。通过 VUEX,你可以更方便地跟踪和调试状态变化,以及在不同组件之间共享状态。无论是大型还是小型的 Vue.js 应用程序,VUEX 都可以帮助你更好地组织和管理应用程序的状态。

     有什么作用

    1. 集中管理状态:VUEX 提供了一个全局的状态容器(store),将应用程序的状态集中存储在一个地方。这样,多个组件可以共享和访问同一个状态,避免了组件之间状态传递的复杂性。

    2. 状态共享:VUEX 允许你在应用程序的不同组件之间共享状态。这意味着,一个组件的状态变化可以被其他组件感知到,并相应地做出更新。这种状态共享可以简化组件之间的通信,提高代码的可维护性。

    3. 易于跟踪和调试状态变化:VUEX 记录了状态的变化历史,使得你可以轻松地跟踪和调试状态的变化。这对于定位和解决状态相关的问题非常有帮助。

    4. 快速的状态更新:通过定义 mutations 和 actions,VUEX 提供了一种统一且可控的方式来更新状态。这样可以确保状态的变化是可追踪和可测试的,也提供了对状态变化的约束和限制。

    总之,VUEX 使得在 Vue.js 应用程序中管理和共享状态变得更加简单和高效,提高了应用的可维护性和可扩展性。 

    取值

    思维图 

     安装

    输入以下命令来安装Vuex:

     npm install vuex -S   (node的环境配置为10的执行这个命令)

     npm i -S vuex@3.6.2  (node的环境配置为18的执行这个命令)

     

     package.json中有这些代码就代表安装成功了

     

    菜单栏

    在src中创建一个vuex的目录,在改目录下创建两个组件demo1,demo2 

    demo1

    1. <template>
    2. <div style="padding: 60px;">
    3. <h1>我是第一个</h1>
    4. <p>改变state中的值</p>
    5. 请输入:<input v-model="msg" />
    6. <button @click="fun1">获取state</button>
    7. <button @click="fun2">改变state</button>
    8. <button @click="fun3">获取state</button>
    9. <button @click="fun4">请求后台</button>
    10. </div>
    11. </template>
    12. <script>
    13. export default {
    14. data() {
    15. return {
    16. msg: '默认值'
    17. }
    18. },
    19. methods: {
    20. fun1() {
    21. // this.$store-->store/index.js 不推荐
    22. let eduName = this.$store.state.eduName;
    23. alert(eduName);
    24. },
    25. fun2() {
    26. this.$store.commit('setEduName', {
    27. eduName: this.msg
    28. })
    29. },
    30. fun3() {
    31. this.$store.dispatch('setEduNameAsync', {
    32. eduName: this.msg
    33. })
    34. },
    35. fun4() {
    36. this.$store.dispatch('setEduNameByAjax', {
    37. eduName: this.msg,
    38. _this:this
    39. })
    40. }
    41. }
    42. }
    43. </script>
    44. <style>
    45. </style>

    demo2

    1. <template>
    2. <div>
    3. <h1>我是第二个</h1>
    4. {{eduName}}
    5. </div>
    6. </template>
    7. <script>
    8. export default{
    9. data(){
    10. return{
    11. msg:'默认值'
    12. }
    13. },
    14. computed:{
    15. eduName(){
    16. return this.$store.state.eduName;
    17. // return this.$store.getters.getEduName;
    18. }
    19. }
    20. }
    21. </script>
    22. <style>
    23. </style>

     到项目中src的router的index.js文件中配置路径

    1. import demo1 from '@/views/vuex/demo1'
    2. import demo2 from '@/views/vuex/demo2'
    3. {
    4. path: '/vuex/demo1',
    5. name: 'demo1',
    6. component: demo1
    7. },{
    8. path: '/vuex/demo2',
    9. name: 'demo2',
    10. component: demo2
    11. }

    在src中的components的LeftNav.vue组件中编辑(增加)代码 

    1. <el-submenu index="idx_999" key="key_999">
    2. <template slot="title">
    3. <span>vuex管理</span>
    4. </template>
    5. <el-menu-item index="/vuex/demo1" key="key_99901">
    6. <span>页面1</span>
    7. </el-menu-item>
    8. <el-menu-item index="/vuex/demo2" key="key_99902">
    9. <span>页面2</span>
    10. </el-menu-item>
    11. </el-submenu>

    模块

    state.js

    1. export default{
    2. eduName:'沸羊羊课堂开课啦',
    3. }

    getters.js 

    1. export default{
    2. setEduName:(state)=>{
    3. return state.eduName;
    4. }
    5. }

     mutations.js

    1. export default{
    2. setEduName:(state,payload)=>{
    3. state.eduName = payload.eduName;
    4. }
    5. }

     index.js

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. import state from './state'
    4. import getters from './getters'
    5. import actions from './actions'
    6. import mutations from './mutations'
    7. Vue.use(Vuex)
    8. const store = new Vuex.Store({
    9. state,
    10. getters,
    11. actions,
    12. mutations
    13. })
    14. export default store

     引用

    在mian.js中引用

    1. // The Vue build version to load with the `import` command
    2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
    3. import Vue from 'vue'
    4. //开发坏境才会引入mockjs
    5. // process.env.MOCK && require('@/mock')
    6. import ElementUI from 'element-ui'
    7. import 'element-ui/lib/theme-chalk/index.css'
    8. import App from './App'
    9. import router from './router'
    10. import store from './store'
    11. Vue.use(ElementUI)
    12. Vue.config.productionTip = false
    13. import axios from '@/api/http'
    14. import VueAxios from 'vue-axios'
    15. Vue.use(VueAxios,axios)
    16. /* eslint-disable no-new */
    17. new Vue({
    18. el: '#app',
    19. router,
    20. store,
    21. data(){
    22. return{
    23. Bus:new Vue()
    24. }
    25. },
    26. components: { App },
    27. template: ''
    28. })

    取值

    1. <template>
    2. <div style="padding: 60px;">
    3. <h1>我是第一个</h1>
    4. <p>改变state中的值</p>
    5. 请输入:<input v-model="msg" />
    6. <button @click="fun1">获取state</button>
    7. <button @click="fun2">改变state</button>
    8. <button @click="fun3">获取state</button>
    9. <button @click="fun4">请求后台</button>
    10. </div>
    11. </template>
    12. <script>
    13. export default {
    14. data() {
    15. return {
    16. msg: '默认值'
    17. }
    18. },
    19. methods: {
    20. fun1() {
    21. // this.$store-->store/index.js 不推荐
    22. let eduName = this.$store.state.eduName;
    23. alert(eduName);
    24. },
    25. fun2() {
    26. this.$store.commit('setEduName', {
    27. eduName: this.msg
    28. })
    29. },
    30. fun3() {
    31. this.$store.dispatch('setEduNameAsync', {
    32. eduName: this.msg
    33. })
    34. },
    35. fun4() {
    36. this.$store.dispatch('setEduNameByAjax', {
    37. eduName: this.msg,
    38. _this:this
    39. })
    40. }
    41. }
    42. }
    43. </script>
    44. <style>
    45. </style>

    异步

    demo1

    1. <template>
    2. <div style="padding: 60px;">
    3. <h1>我是第一个</h1>
    4. <p>改变state中的值</p>
    5. 请输入:<input v-model="msg" />
    6. <button @click="fun1">获取state</button>
    7. <button @click="fun2">改变state</button>
    8. <button @click="fun3">获取state</button>
    9. <button @click="fun4">请求后台</button>
    10. </div>
    11. </template>
    12. <script>
    13. export default {
    14. data() {
    15. return {
    16. msg: '默认值'
    17. }
    18. },
    19. methods: {
    20. fun1() {
    21. // this.$store-->store/index.js 不推荐
    22. let eduName = this.$store.state.eduName;
    23. alert(eduName);
    24. },
    25. fun2() {
    26. this.$store.commit('setEduName', {
    27. eduName: this.msg
    28. })
    29. },
    30. fun3() {
    31. this.$store.dispatch('setEduNameAsync', {
    32. eduName: this.msg
    33. })
    34. },
    35. fun4() {
    36. this.$store.dispatch('setEduNameByAjax', {
    37. eduName: this.msg,
    38. _this:this
    39. })
    40. }
    41. }
    42. }
    43. </script>
    44. <style>
    45. </style>

    demo2

    1. <template>
    2. <div>
    3. <h1>我是第二个</h1>
    4. {{eduName}}
    5. </div>
    6. </template>
    7. <script>
    8. export default{
    9. data(){
    10. return{
    11. msg:'默认值'
    12. }
    13. },
    14. computed:{
    15. eduName(){
    16. return this.$store.state.eduName;
    17. // return this.$store.getters.getEduName;
    18. }
    19. }
    20. }
    21. </script>
    22. <style>
    23. </style>

     在src的action.js中配置后台请求的地址

       'VUEX_AJAX': '/vuex/queryVuex', //Vuex的后台异步请求

    在src的store模块中编写actions.js

    1. export default {
    2. setEduNameByAsync: function(context, payload) {
    3. setTimeout(() => {
    4. //这里的setEduName(事件类型)是指mutations.js中的setEduName事件
    5. context.commit('setEduName', payload);
    6. }, 10000);
    7. //10000是指10秒之后执行这个事件
    8. },
    9. setEduNameByAjax: function(context, payload) {
    10. let _this=payload._this;
    11. //定义后端都请求地址
    12. let url = _this.axios.urls.VUEX_AJAX;
    13. let params = {
    14. resturantName: payload.eduName
    15. }
    16. _this.axios.post(url, params).then(r => {
    17. console.log(r);
    18. }).catch(e => {
    19. console.log(e);
    20. });
    21. }
    22. }

    效果图展示

  • 相关阅读:
    LeetCode栈队列集锦
    操作系统的发展与分类
    Mysql索引原理
    鸿蒙应用开发-初见:入门知识、应用模型
    深聊测开领域之:一文搞懂什么是敏捷测试,如何做敏捷测试,建议先收藏再学习。
    【激光雷达码盘偏摆角的真假点映射关系】
    SpringBoot+ThreadPoolTaskExecutor+mybatis-plus 多线程批量插入大数量级数据
    动力小帆船制作方法简单,电动小帆船制作方法
    第一章-处理器体系结构
    nodejs+vue实验室上机管理系统的设计与实现-微信小程序-安卓-python-PHP-计算机毕业设计
  • 原文地址:https://blog.csdn.net/m0_74934282/article/details/133778572