• 你还不知道怎么使用Vuex?赶紧来看看吧!


    目录

    一、Vuex的核心组成

    为什么要学习Vuex呢?

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

    组件传参的方式我们目前学习了有两种:

    1、子传父 $emit     父传子 props

    2、总线

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

    3、核心组件(第三种):

            ①state.js  存储变量

            ②Getters.js  获取变量值

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

            ④actions.js   改变变量值(异步)

    vuex中默认情况下是不允许使用vue实例,想要请求后台接口,必须将vue实例当成参数从组件那一方传递过来。

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

    Vuex使用步骤

    1、加载依赖  npm install vuex -S 下载Vuex最新版本的依赖

    (注意:小编所使用的版本不同: npm i -S vuex@3.6.2)

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

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

    4、测试Vuex的存储变量的功能

    三、Vuex中的设置及获取变量值

    四、Vuex中的异步同步操作

    五、Vuex后台交互


    一、Vuex的核心组成

    为什么要学习Vuex呢?

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

    相比于总线的优点在于,能够将整个项目的变量进行统一管理

     

    通过前期的知识分享可以了解到,

    组件传参的方式我们目前学习了有两种:

    1、子传父 $emit     父传子 props

    2、总线

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

    官方图解Vuex:

     

     我的个人图解:

     

    通过学习了今日的 Vuex 知识之后,我们又可以了解到一个组件传参的方式:

    3、核心组件(第三种):

            ①state.js  存储变量

                    获取值  this.$store.state.变量值

            ②Getters.js  获取变量值

                     获取值  this.$store.getters.变量值的get方法

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

                     获取值  this.$store.commit("同步的方法",{...(载荷)})

            ④actions.js   改变变量值(异步)

                    获取值  this.$store.dispatch("异步的方法",{...(载荷)})

    vuex中默认情况下是不允许使用vue实例,想要请求后台接口,必须将vue实例当成参数从组件那一方传递过来。

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

    Vuex使用步骤

    1、加载依赖  npm install vuex -S 下载Vuex最新版本的依赖

    (注意:小编所使用的版本不同: npm i -S vuex@3.6.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

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

     

    4、测试Vuex的存储变量的功能

    state.js:

    1. export default {
    2. resName:'阿喵餐馆'
    3. }

    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. Vue.use(Router)
    12. export default new Router({
    13. routes: [
    14. {
    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. {
    35. path: '/LeftNav',
    36. name: 'LeftNav',
    37. component: LeftNav
    38. },
    39. {
    40. path: '/TopNav',
    41. name: 'TopNav',
    42. component: TopNav
    43. },
    44. {
    45. path: '/sys/Articles',
    46. name: 'Articles',
    47. component: Articles
    48. },
    49. {
    50. path: '/sys/VuexPage1',
    51. name: 'VuexPage1',
    52. component: VuexPage1
    53. }
    54. ]
    55. }
    56. ]
    57. })

    VuexPage1.vue:

    1. <script>
    2. export default {
    3. name: 'VuexPage1',
    4. data () {
    5. return {
    6. }
    7. },
    8. computed:{
    9. msg(){
    10. // 从vuex的state文件中获取值
    11. return this.$store.state.resName;
    12. }
    13. }
    14. }
    15. script>
    16. <style scoped>
    17. style>

    运行:

     

    三、Vuex中的设置及获取变量值

     VuexPage2.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. {
    16. path: '/',
    17. name: 'Login',
    18. component: Login
    19. },
    20. {
    21. path: '/Login',
    22. name: 'Login',
    23. component: Login
    24. },
    25. {
    26. path: '/Reg',
    27. name: 'Reg',
    28. component: Reg
    29. },
    30. {
    31. path: '/AppMain',
    32. name: 'AppMain',
    33. component: AppMain,
    34. children:[
    35. {
    36. path: '/LeftNav',
    37. name: 'LeftNav',
    38. component: LeftNav
    39. },
    40. {
    41. path: '/TopNav',
    42. name: 'TopNav',
    43. component: TopNav
    44. },
    45. {
    46. path: '/sys/Articles',
    47. name: 'Articles',
    48. component: Articles
    49. },
    50. {
    51. path: '/sys/VuexPage1',
    52. name: 'VuexPage1',
    53. component: VuexPage1
    54. },
    55. {
    56. path: '/sys/VuexPage2',
    57. name: 'VuexPage2',
    58. component: VuexPage2
    59. }
    60. ]
    61. }
    62. ]
    63. })

     

    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. }

    运行:

    (角色管理)VuexPage2.vue:

     如果点击按钮之后:

     角色管理(VuexPage2)也同样共用了VuexPage1的界面:

     

    四、Vuex中的异步同步操作

    VuexPage1.vue:

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

    actions.js:

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

    效果演示:

    先点按钮最终的店长,然后点按钮盘它:

     

    时隔6秒之后,发生改变: 

     

    五、Vuex后台交互

    actions.js:

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

     

     VuexPage1.vue:

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

    运行:

    Vuex后台交互完成!

    今日分享就到这了,感谢观看!

  • 相关阅读:
    flask 和fastdeploy 快速部署 yolov3
    查询ES之细化需求实现多字段、范围过滤、加权和高亮
    FastestDet---模型训练
    vim的使用快捷键之删除、复制、粘贴
    微服务分布式springcloud的体育场地预约系统演kdm1z
    C++学习记录(三)
    起号1个月后,我分析了一些AI数字人项目的红利期和优缺点
    centos7 配置Nginx 启动 停止
    CPT205-Computer Graphics
    使用Redis源码探究字符串的实现
  • 原文地址:https://blog.csdn.net/weixin_65808248/article/details/126855136