当前位置:   article > 正文

Vuex怎么使用?来看看吧~_vuex的使用

vuex的使用

  

目录

一、Vuex的简介

1、为什么要学习Vuex?

解决了前端组件传参的问题。

组件传参:

        ①子传父:$emit

        ②父传子:props

        ③总线:vue根实例中定义变量,这个变量也是vue实例

2、什么是Vuex?

 官方图解Vuex:

 图解Vuex各组件:

 变量传值的演变方式:

Vuex的核心组件:​​​​​​​

二、Vuex的安装以及store.js的使用

1、vuex使用步骤

        ​​​​​​​1.1 安装

        npm install vuex -s 【下载Vuex最新的版本】

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

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

        1.4 测试Vuex的存储变量的功能

              npm i -S vuex@3.6.2 

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

四、Vuex中的异步同步操作

五、Vuex后台交互


一、Vuex的简介

1、为什么要学习Vuex?

解决了前端组件传参的问题。

组件传参:

        ①子传父:$emit

        ②父传子:props

        ③总线:vue根实例中定义变量,这个变量也是vue实例

2、什么是Vuex?

 官方图解Vuex:

 图解Vuex各组件:

 变量传值的演变方式:

Vuex的核心组件:

        sate.js        存储变量

        Getters.js        获取变量值、

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

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


二、Vuex的安装以及store.js的使用

1、vuex使用步骤

1.1 安装

        npm install vuex -s 【下载Vuex最新的版本】

下载之后【package.json】 里会出现vuex

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

建立store文件,将【actions.js】、【index.js】、【mutations.js】、【state.js】、【getters.js】添加进去

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

1.4 测试Vuex的存储变量的功能

记得更改vuex依赖的版本 运行一下命令

 npm i -S vuex@3.6.2 

state.js

  1. export default{
  2. resName4:'cc咖啡馆'
  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 TopNav from '@/components/TopNav'
  6. import LeftNav from '@/components/LeftNav'
  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. {
  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. }
  55. ]
  56. })

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. console.log(this.$store)
  16. // 从vuex 的 state 文件中获取值
  17. return this.$store.state.resName;
  18. // this.$router.push()...
  19. // this.$root
  20. }
  21. }
  22. }
  23. </script>
  24. <style>
  25. </style>

运行效果展示:


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

是通过方法去获取值。

VuexPage2.vue

  1. <template>
  2. <div>
  3. <p>页面2:欢迎来到{{msg}}</p>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'VuexPage2',
  9. data () {
  10. return {
  11. }
  12. },
  13. computed:{
  14. msg(){
  15. // 从vuex的state文件中获取值
  16. // return this.$store.state.resName;——>不推荐,不安全
  17. // 通过getters.js文件获取state.js中定义的变量值
  18. return this.$store.getters.getResName;
  19. }
  20. }
  21. }
  22. </script>
  23. <style scoped>
  24. </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的方法
  3. setResName:(state,payload)=>{
  4. // sate对象就对应了sate.js中的对象
  5. // payload载荷 对应的 传递的 josn对象参数(name:zs,age:12)
  6. state.resName = payload.resName;
  7. }
  8. }

getters.js

  1. // 拿
  2. export default{
  3. getResName:(state)=>{/* 这里的sate代表的是sate.js整个文件 */
  4. return state.resName;
  5. }
  6. }

效果运行: 点击按钮

 就会出现以下效果:

 

(角色管理)VuexPage2.vue​​​​​​​

 点击按钮 之后:

 


四、Vuex中的异步同步操作

VuexPage1.vue

  1. <template>
  2. <div>
  3. <p>页面1:欢迎来到{{msg}}</p>
  4. <button @click="buy">买下它</button>
  5. <button @click="buyAsync">最终店长</button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'VuexPage1',
  11. data () {
  12. return {
  13. }
  14. },
  15. methods:{
  16. buy(){
  17. // 通过commit方法会调用mutation.js中定义好的方法
  18. this.$store.commit("setResName",{
  19. resName:'KFC'
  20. })
  21. },
  22. buyAsync(){
  23. this.$store.dispatch("setResNameAsync",{
  24. resName:'麦当劳'
  25. })
  26. }
  27. },
  28. computed:{
  29. msg(){
  30. // 从vuex的state文件中获取值
  31. // return this.$store.state.resName; 不推荐,不安全
  32. // 通过 getters.js文件获取 state.js中定义的变量值
  33. return this.$store.getters.getResName;
  34. }
  35. }
  36. }
  37. </script>
  38. <style scoped>
  39. </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. <template>
  2. <div>
  3. <p>页面1:欢迎来到{{msg}}</p>
  4. <button @click="buy">盘它</button>
  5. <button @click="buyAsync">最终的店长</button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'VuexPage1',
  11. data () {
  12. return {
  13. }
  14. },
  15. methods:{
  16. buy(){
  17. //通过commit方法 会 调用 mutations.js文件中定义好的方法
  18. this.$store.commit("setResName",{
  19. resName:'鸡肉味的猫粮'
  20. })
  21. },
  22. buyAsync(){
  23. this.$store.dispatch("setResNameAsync",{
  24. resName:'杨总',
  25. _this:this
  26. })
  27. }
  28. },
  29. computed:{
  30. msg(){
  31. // 从vuex的state文件中获取值
  32. // return this.$store.state.resName;——>不推荐,不安全
  33. // 通过getters.js文件获取state.js中定义的变量值
  34. return this.$store.getters.getResName;
  35. }
  36. }
  37. }
  38. </script>
  39. <style scoped>
  40. </style>

运行:


本篇内容分享到此结束,我们下期再见! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/257070
推荐阅读
相关标签
  

闽ICP备14008679号