Vuex以及axios

Vuex简介

vuex是一个专门为Vue.js设计的集中式状态管理架构。vue

状态? 咱们把它理解为在data中须要共享给其余组件使用的部分。node

Vuex和单纯的全局对象有如下不一样:ios

一、Vuex 的状态存储是响应式的。当vue组件从store中读取状态的时候,vuex

  若store中的状态发生变化,那么相应的组件也会相应的获得高效更新。npm

二、你不能直接改变store中的状态。改变store中的状态的惟一途径就是显示的axios

  提交(commit)mutation。这样使得咱们能够方便的跟踪每个状态的变化,api

  从而让咱们可以实现一些工具来帮助咱们更好的了解咱们的应用。浏览器

安装使用vuex

  --  npm install vuex缓存

// main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import vuex from 'vuex'

Vue.use(vuex)

Vue.config.productionTip = false

const store = new vuex.Store({
    state: {
      show: false,
    }
});

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
});
vuex的使用一
// 为了方便维护,咱们一般把在src下面新建一个store文件夹,
// 而后在里面新建一个index.js
import Vue from 'vue'
import Vue_x from "vuex"

Vue.use(Vue_x);

export default  new Vue_x.Store({
   state: {
      show: false,
    },
});
// 那么main.js要改为
import Vue from 'vue'
import App from './App'
import router from './router'
import store from "./store"

Vue.config.productionTip = false;

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
});
vuex的使用二

State

简而言之~~state是保存咱们data中须要共享的数据。架构

因为Vuex的存储是响应式的,从store实例中读取状态的最简单的方式就是在计算属性中返回某个状态。

this.$store.state.count

// 建立一个组件
const Counter = {
  template: `<div>{{ count }}</div>`,
  computed: {
    count(){
      return this.$store.state.count
    }
  }
};
组件中获取vuex中状态

Getter

有时候咱们须要从store中的state中派生出一些状态,例如对数据进行简单的计算。

而且不少组件都须要用到此方法,咱们要么复制这个函数,要么抽取到一个公共函数,多处导入。

咱们vuex提供了更加方便的方法,getter ,它就像计算属性同样,getter的返回值会根据它的依赖被

缓存起来,只有它的依赖发生改变时,才会从新计算。

Getter会接收state做为其第一个参数:

import Vue from 'vue'
import Vue_x from "vuex"

Vue.use(Vue_x);

export default  new Vue_x.Store({
   state: {
     count: 20,
   },
  // 经过 this.$store.getters.my_func
  getters: {
    my_func: function (state) {
      return state.count * 2
    }
  },

});
Getter使用

Getter也能够接收getters为第二个参数:

import Vue from 'vue'
import Vue_x from "vuex"

Vue.use(Vue_x);

export default  new Vue_x.Store({
   state: {
     count: 20,
   },
  // 经过 this.$store.getters.my_func
  getters: {
    my_func: function (state) {
      return state.count * 2
    },
    // 经过 this.$store.getters.my_func_count
    my_func_count: function (state, getters) {
      return getters.my_func.length
    }
  },

});
Getter使用

Mutation

更改Vuex中的store中的状态的惟一方法是提交mutation。

每一个mutation都有一个字符串的事件类型(type),和一个回调函数handler。

也就是说咱们要触发mutation中定义的方法(type),而后才会执行这个方法(handler)。

这个方法就是咱们更改状态的地方,它会接收state为第一个参数,后面接收其余参数:

import Vue from 'vue'
import Vue_x from "vuex"

Vue.use(Vue_x);

export default  new Vue_x.Store({
   state: {
     count: 20,
   },
  // 须要经过 this.$store.commit('increment', 10)
  mutations: {
     increment (state, n) {
       // 变动状态
       state.count += n
     }
  }

});
Mutation基本使用

Mutation须要遵照Vue的响应规则

既然vuex中的store中的状态是响应式的,那么当咱们状态变动时,监视状态的vue组件也会更新。

这就意味着vuex中的mutation也须要与使用vue同样遵照一些注意事项:

  -- 1,最好提早在你的store中初始化好全部的所须要的属性

  -- 2,当对象须要添加属性时,你应该使用

      --  Vue.set(obj, 'newProp', 123)

      --  以新对象代替老对象  state.obj = { ...state.obj, newProp: 123}

axios的简单使用

基于Promise的HTTP请求客户端,能够同时在浏览器和node.js使用。

使用npm安装axios

  -- npm install axios -D

基本的配置

// main.js
import axios from "axios"

Vue.prototype.$axios = axios

// 组件中
methods: {
     init () {
        this.$axios({
             method: "get",
             url: "/user"
        })
    },
};
axios的基本配置

基本的使用

test(){
          this.$axios.get(this.$store.state.apiList.course,{
            params: {
              id: 123,
            }
          }).then(function (response) {
            // 请求成功回调函数
          }).catch(function (response) {
            // 请求失败的回调函数
          })
}
get请求
test(){
          this.$axios.post(this.$store.state.apiList.course,{
              course_title: "Python",
              course_price: "19.88"
          }).then(function (response) {
            // 请求成功回调函数
          }).catch(function (response) {
            // 请求失败的回调函数
          })
}
post请求
function getCourse(){
          return this.$axios.get('/course/12')
        }
function getCourse_all() {
          return this.$axios.get('/course')
        }
this.$axios.all([getCourse_all(),getCourse()])
          .then().catch()
发送多个并发请求
methods: {
          init(){
            var that = this
            this.$axios.request({
              url: that.$store.state.apiList.course,
              method: 'get'
            }).then(function (data) {
              if (data.status === 200){
                  that.courseList = data.data
              }
            }).catch(function (reason) {
              console.log(reason)
            })
          }
},
axios.request 本人喜欢的
相关文章
相关标签/搜索