Vue10:简单使用Vuex

本节学习目标:

1、了解vuex中的各个js文件的用途

2、利用vuex同步存值

3、利用vuex取值

4、Vuex的异步加载问题及后台调用问题

一,Vuex中的各个js文件的用途

1.变量传值的演变形式

 图解Vuex各组件的作用

官方图解Vuex

 


2. vue中各个组件之间传值


  2.1父子组件
   父组件-->子组件,通过子组件的自定义属性:props
   子组件-->父组件,通过自定义事件:this.$emit('事件名',参数1,参数2,...);

 2.2非父子组件或父子组件
   通过数据总数Bus,this.$root.$emit('事件名',参数1,参数2,...)

 2.3非父子组件或父子组件
   更好的方式是在vue中使用vuex

方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据
 

二、 Vuex 

官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库), 让其在各个页面上实现数据的共享包括状态,并且可操作

 Vuex分成五个部分: 
   1.State:单一状态树
   2.Getters:状态获取
   3.Mutations:触发同步事件
   4.Actions:提交mutation,可以包含异步操作
   5.Module:将vuex进行分模块

 vuex使用步骤

1、安装
npm i -S vuex@3.6.2

 补充:vue对应版本为2.0版本,则vuex使用3.0版本

            vue对应版本为3.0版本,则vuex使用4.0版本

            vue的2.14.0版本是临界点

2、创建store模块,分别维护state/actions/mutations/getters

 3、store/index.js文件中新建vuexstore实例,并注册上面引入的各大模块
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'
Vue.use(Vuex)
const store = new Vuex.Store({
 	state,
 	getters,
 	actions,
 	mutations
 })

 export default store
4、main.js中导入并使用store实例
import store from './store'
new Vue({
  el: '#app',
  router,
//添加store
  store,
  components: { App },
  template: '<App/>',//index.html
  data(){
    return{
        Bus: new Vue({})
    }
  }
})
5、示例(同步操作)
5.1在vuex中添加两个组件(views/vuex/Page1.vue)

 5.2配置路由(router/index.js)
//前面导入Page1.vue,Page2.vue
import Page1 from '@/views/vuex/Page1'
import Page2 from '@/views/vuex/Page2'


//routes中加入
{
    path: '/vuex/Page1',
    name: 'Page1',
    component: Page1
},
{
    path: '/vuex/Page2',
    name: 'Page2',
    component: Page2
}
LeftNav.vue中加入
<div class="logobox">
      <img class="logoimg" src="../assets/img/logo.png" alt="">
    </div>

    <!-- vuex示例 -->
    <el-menu-item index="/vuex/Page1" key="key_999">
        <i class=""></i>
        <span>vuex页面1</span>
    </el-menu-item>
    <el-menu-item index="/vuex/Page2" key="key_1000">
        <i class=""></i>
        <span>vuex页面2</span>
    </el-menu-item>
5.3启动服务器,得到运行结果

 

 5.4state直接取值:

state.js

export default{
  name:'hjl'
}

Page1取到值的时候,Page2也获取到了值,证明主键传参

Page1.vue

<template>
  <div>
    <h1>这是页面1==={{msg}}</h1>
    <p>state直接取值</p>
    <button @click="func1">state直接取值</button>
  </div>
</template>

<script>
  export default{
    data(){
      return{
        msg:null
      }
    },
    methods:{
      func1(){
        //不建议使用
        this.msg = this.$store.state.name;
      }
    }
  }
</script>

<style>
</style>

Page2.vue

<template>
  <div>
    <h2>这是页面2===={{msg}}</h2>
  </div>
</template>

<script>
  export default{
    data(){
      return{
        //msg:null
      }
    },
    // created(){
    //   this.msg = this.$store.state.name;
    // }
    computed:{
      msg(){
        return this.$store.state.name;
      }
    }
  }
</script>

<style>
</style>

效果:

 

 5.4getters取值:

Page1.vue:

<template>
  <div>
    <h1>这是页面1==={{msg}}</h1>
    <p>state直接取值</p>
    <button @click="func1">state直接取值</button>
    <p>getters取值</p>
    <button @click="func2">getters取值</button>
  </div>
</template>

<script>
  export default{
    data(){
      return{
        msg:null
      }
    },
    methods:{
      func1(){
        //不建议使用
        this.msg = this.$store.state.name;
      }
      func2(){
        this.msg = this.$store.getters.getName;
      }
    }
  }
</script>

<style>
</style>

getters.js

export default{
  getName:function(state){
    return state.name;
  }
}

效果与之前一样 

 5.5mutation改变值

Page1.vue:

<template>
  <div>
    <h1>这是页面1==={{msg}}</h1>
    <p>state直接取值</p>
    <button @click="func1">state直接取值</button>
    <p>getters取值</p>
    <button @click="func2">getters取值</button>
    <p>mutations取值</p>
    <button @click="func3">mutations改变值</button>
  </div>
</template>

<script>
  export default{
    data(){
      return{
        //msg:null
      }
    },
    //快速加载,实时监控
    computed:{
      msg(){
        return this.$store.state.name;
      }
    },
    methods:{
      func1(){
        //不建议使用
        this.msg = this.$store.state.name;
      },
      func2(){
        this.msg = this.$store.getters.getName;
      },
      func3(){
        this.$store.commit('setName',{name:'hjlplus'});
      }
    }
  }
</script>

<style>
</style>

mutation.js

export default{
  setName:function(state,payload){
    //payload:载荷
    state.name = payload.name;
  }
}

**加载优化:

        computed:{
              msg(){
                        return this.$store.state.name;
              }
            }, 

6、示例(异步操作) 

Page1.vue:

<template>
  <div>
    <h1>这是页面1==={{msg}}</h1>
    <p>state直接取值</p>
    <button @click="func1">state直接取值</button>
    <p>getters取值</p>
    <button @click="func2">getters取值</button>
    <p>mutations取值</p>
    <button @click="func3">mutations改变值</button>
    <p>actions取值</p>
    <button @click="func4">actions改变值</button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        //msg:null
      }
    },
    //快速加载,实时监控
    computed: {
      msg() {
        return this.$store.state.name;
      }
    },
    methods: {
      func1() {
        //不建议使用
        this.msg = this.$store.state.name;
      },
      func2() {
        this.msg = this.$store.getters.getName;
      },
      func3() {
        this.$store.commit('setName', {
          name: 'hjlplus'
        });
      },
      func4() {
        this.$store.dispatch('setNameAsync', {
          name: 'hjlplus---SSS'
        });
      }
    }
  }
</script>

<style>
</style>

actions.js

export default{
  setNameAsync:function(context,payload){
    // context指的是vue实例
    // 等价于this.$store
    setTimeout(function(){
      context.commit('setName',payload);
    },6000);
  }
}

 

 

三、actions中发送Ajax获取后台数据

Page1.vue:

<template>
  <div>
    <h1>这是页面1==={{msg}}</h1>
    <p>state直接取值</p>
    <button @click="func1">state直接取值</button>
    <p>getters取值</p>
    <button @click="func2">getters取值</button>
    <p>mutations取值</p>
    <button @click="func3">mutations改变值</button>
    <p>actions取值</p>
    <button @click="func4">actions改变值</button>
    <p>后台Ajax取值</p>
    <button @click="func5">后台Ajax改变值</button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        //msg:null
      }
    },
    //快速加载,实时监控
    computed: {
      msg() {
        return this.$store.state.name;
      }
    },
    methods: {
      func1() {
        //不建议使用
        this.msg = this.$store.state.name;
      },
      func2() {
        this.msg = this.$store.getters.getName;
      },
      func3() {
        this.$store.commit('setName', {
          name: 'hjlplus'
        });
      },
      func4() {
        this.$store.dispatch('setNameAsync', {
          name: 'hjlplus---SSS'
        });
      },
      func5() {
        this.$store.dispatch('setNameAsync', {
          _this:this
        });
      }
    }
  }
</script>

<style>
</style>

action.js

'VUEX_INFO': '/vuex/queryVuex',//vuex异步获取数据

actions.js

export default{
  setNameAsync:function(context,payload){
    // context指的是vue实例
    // 等价于this.$store
    setTimeout(function(){
      context.commit('setName',payload);
    },3000);
  },
  setNameAjax:function(context,payload){
    let _this = payload._this;
    let url = _this.axios.urls.VUEX_INFO;
    let params ={
      resturantName:'修改为hjl'
    }
    _this.axios.get(url,{params:params}).then(resp=>{
        if(resp.data.success){
          context.commit('setName',{
            name:resp.data.msg
          })
        }
    }).catch(err=>{
      // console.log()
    })
  }
}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值