• 快速上手Vuex


    目录

    前言:

     why:(为什么要使用Vuex)

    1.Vuex的核心组成

    1.1图解Vue各组件

     1.2 核心组件

    state.js 存储变量

    Getters.js 获取变量值

    mutations.js 改变变量值(同步)

    action.js 改变变量值(异步)

    2.Vuex版本问题及store.js的使用

    2.1 安装:

     npm install vuex -S

    npm i -S vuex@3.6.2

    2.2 使用Vuex的步骤

     2.2.1 下载依赖 npm install vuex -s  下载Vuex最新版本的依赖(注意)

    2.2.2 导入Vuex的核心4个组件,然后通过index.js加载进来

     2.2.3.将Vuex对应的index.js挂载到main.js中的vue实例中

    2.2.4.测试Vue的存储变量的功能

    3.Vue中的设置及获取变量值

    4.Vue中的异步同步操作

    5.Vue后台交互


    前言:

     why:(为什么要使用Vuex)

    Vuex解决了前端组件传参的问题,针对当前项目所有的变量进行统一管理,可以理解为前端的数据库

    组件传参

    1.子传父  $emit,父传子 props

    2.总线

       vue根实例中定义变量,这个变量也是vue实例

    1.Vuex的核心组成

    1.1图解Vue各组件

    由四个部分组成

     1.2 核心组件

    state.js 存储变量

    Getters.js 获取变量值

    mutations.js 改变变量值(同步)

    action.js 改变变量值(异步)

    2.Vuex版本问题及store.js的使用

    2.1 安装:

     npm install vuex -S

    npm i -S vuex@3.6.2

    下载完成,打开 package.json查看版本为:4.0.2

     然后我们开始使用它

    2.2 使用Vuex的步骤

    2.2.1 下载依赖 npm install vuex -s  下载Vuex最新版本的依赖(注意)

    2.2.2 导入Vuex的核心4个组件,然后通过index.js加载进来

             index.js

            state.js

            actions.js

            mutations.js

            getters.js

     2.2.1 下载依赖 npm install vuex -s  下载Vuex最新版本的依赖(注意)

    2.2.2 导入Vuex的核心4个组件,然后通过index.js加载进来

     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

     2.2.3.将Vuex对应的index.js挂载到main.js中的vue实例中

    2.2.4.测试Vue的存储变量的功能

    启动后端

     启动前端

     打开项目的用户管理:路径为 localhost:8083/#/sys/VuexPage1

    新建一个页面用作于用户管理

    /sys/VuexPage1

    1. <template>
    2. <div>
    3. <p>欢迎来到{{msg}}p>
    4. div>
    5. template>
    6. <script>
    7. export default{
    8. name:'VuexPage1',
    9. data(){
    10. return {
    11. }
    12. },
    13. computed:{
    14. msg(){
    15. //从vuex的state 文件中获值
    16. return this.$store.state.resName;
    17. //this.$router.push()..
    18. //this.$root.But.$on()
    19. }
    20. }
    21. }
    22. script>
    23. <style>
    24. style>

     index.js:

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import AppMain from '@/components/AppMain'
    5. import LeftNav from '@/components/LeftNav'
    6. import TopNav from '@/components/TopNav'
    7. import Login from '@/views/Login'
    8. import Reg from '@/views/Reg'
    9. import Articles from '@/views/sys/Articles'
    10. import VuexPage1 from '@/views/sys/VuexPage1'
    11. Vue.use(Router)
    12. export default new Router({
    13. routes: [{
    14. path: '/',
    15. name: 'Login',
    16. component: Login
    17. },
    18. {
    19. path: '/Login',
    20. name: 'Login',
    21. component: Login
    22. },
    23. {
    24. path: '/Reg',
    25. name: 'Reg',
    26. component: Reg
    27. },
    28. {
    29. path: '/AppMain',
    30. name: 'AppMain',
    31. component: AppMain,
    32. children: [{
    33. path: '/LeftNav',
    34. name: 'LeftNav',
    35. component: LeftNav
    36. },
    37. {
    38. path: '/TopNav',
    39. name: 'TopNav',
    40. component: TopNav
    41. },
    42. {
    43. path: '/sys/Articles',
    44. name: 'Articles',
    45. component: Articles
    46. },
    47. {
    48. path: '/sys/VuexPage1',
    49. name: 'VuexPage1',
    50. component: VuexPage1
    51. }
    52. ]
    53. }
    54. ]
    55. })

     state.js

     

     再次打开项目查看,内容并没有展示

     在页面点击F12查看报错,发现$store未定义

    不同浏览器可能错误提示有所差异,也可能是

     

    我们查看代码中的报错部分,为了排查错误,我们将报错的代码先注释,然后返回一个常量

     返回网页:内容出现了

     此时我们之前所有的操作都是正确的,但是为什么会发生这种情况呢?

    小刘在网上搜索了很多的资料 发现有可能是版本的问题

    此时我们下载  npm i -S vuex@3.6.2 版本

     

     此时我们的版本就已经发生了改变,然后我们重新使用指令 npm run dev 启动项目

     此时,我们的效果就出来了!!!

    以上就是关于版本方面的问题,大家如果遇到这种情况也可以向这方面思考

    3.Vue中的设置及获取变量值

    更改VuePage1.Vue:

    1. <script>
    2. export default{
    3. name:'VuexPage1',
    4. data(){
    5. return {
    6. }
    7. },
    8. methods:{
    9. buy(){
    10. console.log(this.$store);
    11. //通过commit 方法 会调用 mutations.js文件中定义好的方法
    12. this.$store.commit("setResName",{
    13. resName:'KFC'
    14. })
    15. }
    16. }
    17. ,
    18. computed:{
    19. msg(){
    20. console.log(this.$store);
    21. //从vuex的state 文件中获值
    22. // return this.$store.state.resName;//不推荐 不安全
    23. //通过 getter.js 文件获取 state.js 中定义的变量值
    24. return this.$store.getters.getResName;
    25. // return 'xxx';
    26. // //this.$router.push()..
    27. //this.$root.But.$on()
    28. }
    29. }
    30. }
    31. script>
    32. <style>
    33. style>

    VuePage2.Vue:

    1. <script>
    2. export default {
    3. name: 'VuexPage2',
    4. data () {
    5. return {
    6. }
    7. },
    8. computed:{
    9. msg(){
    10. // 从vuex的state文件中获取值
    11. // return this.$store.state.resName;——>不推荐,不安全
    12. // 通过getters.js文件获取state.js中定义的变量值
    13. return this.$store.getters.getResName;
    14. }
    15. }
    16. }
    17. script>
    18. <style scoped>
    19. style>

     router/index.js:

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import AppMain from '@/components/AppMain'
    5. import LeftNav from '@/components/LeftNav'
    6. import TopNav from '@/components/TopNav'
    7. import Login from '@/views/Login'
    8. import Reg from '@/views/Reg'
    9. import Articles from '@/views/sys/Articles'
    10. import VuexPage1 from '@/views/sys/VuexPage1'
    11. import VuexPage2 from '@/views/sys/VuexPage2'
    12. Vue.use(Router)
    13. export default new Router({
    14. routes: [{
    15. path: '/',
    16. name: 'Login',
    17. component: Login
    18. },
    19. {
    20. path: '/Login',
    21. name: 'Login',
    22. component: Login
    23. },
    24. {
    25. path: '/Reg',
    26. name: 'Reg',
    27. component: Reg
    28. },
    29. {
    30. path: '/AppMain',
    31. name: 'AppMain',
    32. component: AppMain,
    33. children: [{
    34. path: '/LeftNav',
    35. name: 'LeftNav',
    36. component: LeftNav
    37. },
    38. {
    39. path: '/TopNav',
    40. name: 'TopNav',
    41. component: TopNav
    42. },
    43. {
    44. path: '/sys/Articles',
    45. name: 'Articles',
    46. component: Articles
    47. },
    48. {
    49. path: '/sys/VuexPage1',
    50. name: 'VuexPage1',
    51. component: VuexPage1
    52. },
    53. {
    54. path: '/sys/VuexPage2',
    55. name: 'VuexPage2',
    56. component: VuexPage2
    57. }
    58. ]
    59. }
    60. ]
    61. })

    mutations.vue:

    1. export default {
    2. setResName:(state,payload)=>{
    3. // state对象就对应了state.js中的对象
    4. // payload载荷 对应的 传递的 json对象参数{name:zs,age:24}
    5. state.resName=payload.resName;
    6. }
    7. }

     getters.js:

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

    查看界面效果:

    页面1:

     页面2:

     当页面1点击了按钮之后:页面1发生变化

     此时我们再查看页面2:同样发生了改变

    4.Vue中的异步同步操作

    VuexPage1.vue:

    1. <script>
    2. export default{
    3. name:'VuexPage1',
    4. data(){
    5. return {
    6. }
    7. },
    8. methods:{
    9. buy(){
    10. console.log(this.$store);
    11. //通过commit 方法 会调用 mutations.js文件中定义好的方法
    12. this.$store.commit("setResName",{
    13. resName:'KFC'
    14. })
    15. },
    16. buyAsync(){
    17. this.$store.dispatch("setResNameAsync",{
    18. resName:'麦当劳'
    19. })
    20. }
    21. }
    22. ,
    23. computed:{
    24. msg(){
    25. console.log(this.$store);
    26. //从vuex的state 文件中获值
    27. // return this.$store.state.resName;//不推荐 不安全
    28. //通过 getter.js 文件获取 state.js 中定义的变量值
    29. return this.$store.getters.getResName;
    30. // return 'xxx';
    31. // //this.$router.push()..
    32. //this.$root.But.$on()
    33. }
    34. }
    35. }
    36. script>
    37. <style>
    38. style>

    actions.js:

    1. export default{
    2. setResNameAsync:(context,payload)=>{
    3. //异步修改值 在异步方法中调用了同步方法
    4. //context值的是Vue的上下文,相当于 this.$store
    5. setTimeout(function(){
    6. context.commit("setResName",payload);
    7. },6000);
    8. }
    9. }

    效果演示:

    先点击最终店长,此时会没有反应

     我们再点击 “盘它”,

     可以看到的现象:在点击了最终店长的六秒之后,会切换为“麦当劳”

     此时我们的同步跟异步是同时在进行的

    5.Vue后台交互

    actions.js:

    1. export default{
    2. setResNameAsync:(context,payload)=>{
    3. //异步修改值 在异步方法中调用了同步方法
    4. //context值的是Vue的上下文,相当于 this.$store
    5. setTimeout(function(){
    6. context.commit("setResName",payload);
    7. },6000);
    8. let _this=payload._this;
    9. let url=_this.axios.urls.SYSTEM_MENU_TREE;
    10. _this.axios.post(url,{}).then(r=>{
    11. console.log(r);
    12. }).catch(e=>{
    13. });
    14. }
    15. }

    VuexPage1.vue

     获取到后台数据:

  • 相关阅读:
    redis 安装
    PMP每日一练 | 考试不迷路-11.12(包含敏捷+多选)
    纯 CSS 实现瀑布流布局的方法
    七、商城(中级)项目报错汇总
    C++进阶--2
    疯狂星期四,但是程序员
    Http和Https
    微服务节流控制:Eureka中服务速率限制的精妙配置
    Part7:Pandas 的SettingWithCopyWarning 报警复现、原因、解决方案
    STM32单片机学习3--STM32控制键盘
  • 原文地址:https://blog.csdn.net/weixin_64313980/article/details/126855439