• Vuex的使用


    目录

    一、Vuex的核心组成

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

    版本问题

    store/index.js

    router/index.js

    main.js

    state.js

    VuexPage1.vue

    效果

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

    mutations.js

    getters.js

     VuexPage1.vue

    效果1

      VuexPage2.vue

    router/index.js

    效果2

    四、Vuex中同步异步的操作

     actions.js

    VuexPage1.vue

    效果

    五、Vuex后台交互

      VuexPage1.vue

    actions.js

    效果

    一、Vuex的核心组成

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

    版本问题

     此时运行的是npm install vuex -S  对应的版本是4.0.2 因为和vuex不兼容 所以会报错

    换成npm i -S vuex@3.6.2

     

     就能用了

    store/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

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

    main.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. //process.env.MOCK 为false,那么require('@/mock')不执行 process.env.MOCK 在生产环境下为false
    5. //process.env.MOCK && require('@/mock') 因为与后台建立连接,不是用mock模拟数据了
    6. import ElementUI from 'element-ui' // 新添加 1
    7. import 'element-ui/lib/theme-chalk/index.css' // 新添加 2 ,避免后期打包样式不同,要放在import App from './App'; 之前
    8. import App from './App'
    9. import router from './router'
    10. import axios from '@/api/http' // #vue项目对axios的全局配置    
    11. //import axios from 'axios'  
    12. import VueAxios from 'vue-axios'
    13. import store from './store'
    14. Vue.use(ElementUI)   // 新添加 3 使用ElementUI
    15. Vue.use(VueAxios,axios)
    16. Vue.config.productionTip = false
    17. /* eslint-disable no-new */
    18. new Vue({
    19. el: '#app',
    20. data(){
    21. return{
    22. Bus:new Vue({//总线
    23. })
    24. }
    25. },
    26. router,
    27. store,
    28. components: { App },
    29. template: ''
    30. })

    state.js

    1. export default {
    2. resName:'小陈餐馆'
    3. };

    VuexPage1.vue

    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. //从vue的state文件中获取值
    16. return this.$store.state.resName;
    17. }
    18. }
    19. }
    20. script>
    21. <style>
    22. style>

    效果

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

    mutations.js

    1. export default {//修改变量值 同步
    2. setResName:(state,payload)=>{
    3. //state对象就对应了state.js中的对象
    4. //payload载荷对应的传递的Jason对象参数
    5. state.resName = payload.resName;
    6. }
    7. };

    getters.js

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

     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:'KFC'
    13. })
    14. }
    15. },
    16. computed:{
    17. msg(){
    18. //从vue的state文件中获取值
    19. // return this.$store.state.resName; 不推荐不安全
    20. //通过getters.js获取state.js中定义的变量值
    21. return this.$store.getters.getResName;
    22. }
    23. }
    24. }
    25. script>
    26. <style>
    27. style>

    效果1

     

      VuexPage2.vue

    1. <script>
    2. export default {
    3. name: 'VuexPage2',
    4. data () {
    5. return {
    6. }
    7. },
    8. computed:{
    9. msg(){
    10. //从vue的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>
    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. })

    效果2

    四、Vuex中同步异步的操作

     actions.js

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

    VuexPage1.vue

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

    效果

    点击最终店长 等6秒变为如下图

     点击角色管理

    五、Vuex后台交互

      VuexPage1.vue

    1. <script>
    2. export default {
    3. name: "VuexPage1",
    4. data() {
    5. return {};
    6. },
    7. methods: {
    8. buy() {
    9. //通过commit方法会调用mutations.js文件中定义好的方法
    10. this.$store.commit("setResName", {
    11. resName: "KFC"
    12. });
    13. },
    14. buyAsync() {
    15. this.$store.dispatch("setResNameAsync", {
    16. resName: "华莱士",
    17. _this:this
    18. });
    19. }
    20. },
    21. computed: {
    22. msg() {
    23. //从vue的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>style>

    actions.js

    1. export default {
    2. setResNameAsync: (context, payload) => {
    3. //异步修改值 在异步方法中调了同步方法
    4. //context是指vuex的上下文相当于this.$store
    5. setTimeout(function(){//6秒后执行
    6. context.commit("setResName",payload);
    7. },6000);
    8. //vuex后台交互
    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. };

    效果

     注意:必须是_this 而不能是this,若直接用this会拿不到axios

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

    总结:

    ①下载vuex的依赖 npm install vuex -S 是最新版本 4.0.2

    我们需要是npm i -S vuex@3.6.2

    ②导入vuex核心的四个组件,通过index.js加载进来

    ③将vuex对应的index.js挂载到main.js的vue实例中

    ④测试vuex的存储变量的功能

  • 相关阅读:
    vim复制代码包含注释时格式会乱掉的解决办法
    C++中的cout.setf(ios::fixed)是什么意思?
    uniapp怎么进行页面的跳转
    JavaSE 基础(十三)网络编程
    React Hooks—— context hooks
    分布式系统的 38 个知识点
    Linux学习之:进程概念
    Springboot 指定自定义模板导出Excel文件
    股票接口的推出对于散户有哪些意义?
    AtCoder Beginner Contest 354 (ABCDEFG题)视频讲解
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126857554