Vuex+axios

 

Vuex简介

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

状态? 我们把它理解为在data中需要共享给其他组件使用的部分。

Vuex和单纯的全局对象有以下不同:

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

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

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

  提交(commit)mutation。这样使得我们可以方便的跟踪每一个状态的变化,

  从而让我们能够实现一些工具来帮助我们更好的了解我们的应用。

安装使用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 本人喜欢的

 

Vuex+axios的更多相关文章

  1. vue全家桶项目搭建(vue-cli 2.9.6+vue-router+vuex+axios)

    一.安装vue-cli + vue-router + vuex + axios 1.安装vue-cli 2.创建项目 3.安装vuex和axios 二.搭建项目目录结构,如下所示: 1.assets目 ...

  2. 基于 vue+vue-router+vuex+axios+koa+koa-router 本地开发全栈项目

    因为毕业设计要做基于Node服务器的项目,所以我就想着用刚学的vue作为前端开发框架,vue作为Vue.js应用程序的状态管理模式+库,axios基于promise用于浏览器和node.js的http ...

  3. Vue项目中使用Vuex + axios发送请求

    本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...

  4. vue+vuex+axios+echarts画一个动态更新的中国地图

    一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china- ...

  5. vue2.0+webpack+vuerouter+vuex+axios构建项目基础

    前言 本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础 步骤 1.全局安装webpack,命令 npm install webpack -g 注意,web ...

  6. vue全家桶(Vue+Vue-router+Vuex+axios)(Vue+webpack项目实战系列之二)

    Vue有多优秀搭配全家桶做项目有多好之类的咱就不谈了,直奔主题. 一.Vue 系列一已经用vue-cli搭建了Vue项目,此处就不赘述了. 二.Vue-router Vue的路由,先献上文档(http ...

  7. Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...

  8. Vue(二十三)vuex + axios + 缓存 运用 (以登陆功能为例)

    (一)axios 封装 (1)axios拦截器 可以在axios中加入加载的代码... (2)封装请求 后期每个请求接口都可以写在这个里面... (二)vuex user.js import { lo ...

  9. vue+vuex+axios从后台获取数据存入vuex,组件之间共享数据

    在vue项目中组件间相互传值或者后台获取的数据需要供多个组件使用的情况很多的话,有必要考虑引入vuex来管理这些凌乱的状态,今天这边博文用来记录这一整个的过程,后台api接口是使用webpack-se ...

随机推荐

  1. win10系统使用clover时程序崩溃的解决

    1. 工具 --->  Internet选项 2. 程序选项卡 ---> 管理加载项 3.选择ExporerWatcher Class ---> 启用 win10对于未验证的程序状态 ...

  2. line -1: Validation of SOAP-Encoded messages not supported

    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" ...

  3. html5实现进度条功能效果非常和谐

    1. [图片] html5.jpg ​2. [代码][HTML]代码  <script type="text/javascript">    var i = 0;    ...

  4. JS 删除数组中指定的某个元素的方法

    //首先创建函数方法 Array.prototype.indexOf = function(val){ for(var i=0;i<this.length;i++){ if(this[i] == ...

  5. EOF的使用

    1.我疑惑了 char a[20]; while(scanf("%s",a)!=EOF){ cout<<"hello"<<endl; } ...

  6. python raw string

    path = r'C:\a\b\c.txt' r'字符串' 是raw 字符串的意思, 其中的字符串不会转义,即不解释 \ . 作用之一:可以用来保存Windows的路径,直接从资源管理器复制来粘贴,不 ...

  7. STL容器特征总结与迭代器失效

    Vector 内部数据结构:连续存储,例如数组. 随机访问每个元素,所需要的时间为常量. 在末尾增加或删除元素所需时间与元素数目无关,在中间或开头增加或删除元素所需时间随元素数目呈线性变化. 可动态增 ...

  8. CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)

    Your friend is developing a computer game. He has already decided how the game world should look lik ...

  9. kettle监控销售人员当月每天任务完成率_20161107周一

    1.上面是目标表,其中激活客户数为当月每天之前30天未下单的客户 2.写SQL SELECT a.销售员,c.当月销售确认额,a.当月订单额,b.当月首单数,b.当月激活数, a1,b.b1,b.c1 ...

  10. 「UVA1636」Headshot(概率

    题意翻译 你有一把枪(左轮的),你随机装了一些子弹,你开了一枪,发现没有子弹,你希望下一枪也没有子弹,你是应该直接开一枪(输出"SHOOT"),还是先转一下,再开一枪(输出&quo ...