axios和vuex
0.babel
将es6代码转换成各个浏览器都能识别的代码
一.axios
1.官方网站
https://www.kancloud.cn/yunye/axios/234845
2.引用:
(1)cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
(2)下载到当前项目***
npm install axios
3.用法
(1)get请求
// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
}); // 可选地,上面的请求可以这样做
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
(2)post请求
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone' })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
4.使用实例
在main.js中,将Axios挂载到 Vue原型上,每一个子组件都能够使用
import Axios from 'axios' Vue.prototype.$https = Axios Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';
二.vuex
作用:组件之间的传值,任何一个组件都能共享state中的数据
下载vuex到该组件:npm i vuex -S
1.vuex商店的创建
在main.js中
import Vue from 'vue'
import App from './App'
import router from './router/index' //1.引入
import Vuex from "vuex"
//2.组件使用要use
Vue.use(Vuex)
//3.创建一个store实例
const store=new Vuex.Store({
state:{},
mutations:{},
actions:{},
}) new Vue({
el: '#app',
router,
//4.挂载store实例
store,
components: { App},
template: '<div><App></App></div>'
})
2.值的使用
<template>
<div class="">
//2.使用
<h3>我是主页,我是父组件中的{{ num }}</h3> </div>
</template> <script>
export default {
name: 'Home',
data () {
return { }
},
//监听
computed:{
//1.定义方法,调用store,返回store中的值
num:function(){
return this.$store.state.num
}
}
}
</script> <style > </style>
3.值的同步修改
在main.js
import Vue from 'vue'
import App from './App'
import router from './router/index' // Vue.config.productionTip = false import Vuex from "vuex"
Vue.use(Vuex)
const store=new Vuex.Store({
state:{num:111},
mutations:{
//1.定义如何修改
setNum(state,val){
state.num+=val
}
},
actions:{},
}); new Vue({
el: '#app',
router,
//4.挂载store实例
store,
components: { App},
template: '<div><App></App></div>'
});
在Course.vue中
<template>
<div>我是课程
<button @click="setnum">+1按钮</button>
我是父组件中的{{ num }}
</div>
</template>
<script>
export default{
methods:{
//2.确定修改
setnum(){
this.$store.commit("setNum",1)
}
},
computed:{
num:function(){
return this.$store.state.num
}
}
}
</script>
<style></style>
4.值的异步修改
在main.js中
import Vue from 'vue'
import App from './App'
import router from './router/index' import Vuex from "vuex"
Vue.use(Vuex)
const store=new Vuex.Store({
state:{num:111},
mutations:{
setMutaNum(state,val){
state.num+=val
},
setMutaAsync:function(state,val){
state.num+=val
}
},
actions:{
setActionNum(context,val){
context.commit('setMutaNum',val)
},
setActionAsync(context,val){
setTimeout(()=>{
context.commit('setMutaAsync',val)
})
}
},
}); new Vue({
el: '#app',
router,
//4.挂载store实例
store,
components: { App},
template: '<div><App></App></div>'
});
在course.vue中
<template>
<div>我是课程
<button @click="setnum">同步+1按钮</button>
<button @click="setAsynanum">异步 +5按钮</button>
我是父组件中的{{ num }}
</div>
</template>
<script>
export default{
methods:{
setnum(){
this.$store.dispatch("setActionNum",1)
},
setAsynanum(){
this.$store.dispatch("setActionAsync",5)
}
},
computed:{
num:function(){
return this.$store.state.num
}
}
}
</script>
<style></style>
流程如下

axios和vuex的更多相关文章
- day 84 Vue学习六之axios、vuex、脚手架中组件传值
Vue学习六之axios.vuex.脚手架中组件传值 本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的 ...
- day 87 Vue学习六之axios、vuex、脚手架中组件传值
本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的使用 Axios 是一个基于 promise 的 HT ...
- Vue(5)- axios、vuex
一.内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loade ...
- Vue2基于Axios Ajax Vuex的Loading组件
1. 定义根state:ajaxIsLoading2. 在Axios拦截器中commit不同的状态实现状态切换3. 组件中通过getter获取ajaxIsLoading状态 Axios 拦截器配置 i ...
- Vue 5 -- axios、vuex
一.内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loade ...
- 【实战】Vue全家桶(vue + axios + vue-router + vuex)搭建移动端H5项目
使用Vue全家桶开发移动端页面. 本博文默认已安装node.js. github链接 一.准备工作 安装vue npm install vue 安装脚手架vue-cli npm install -g ...
- [前端] VUE基础 (9) (element-ui、axios、Vuex)
一.element-ui的使用 官方网页:https://element.eleme.cn/#/zh-CN 1.安装element-ui (venv) D:\pycharm_workspace\vue ...
- Vuex与axios介绍
Vuex集中式状态管理里架构 axios (Ajax) Vuex集中式状态管理架构 -简单介绍: vuex是一个专门为Vue.js设计的集中式状态管理架构. 我们把它理解为在data中需要共享给其他组 ...
- vue-x和axios的学习
axios的使用 使用原因:因为vue本身就带有处理dom的功能,不希望再有其他操作dom的插件,所以用axios代替了jquery 功能:发送xhr请求 下载: $ npm install axio ...
随机推荐
- 嵌套函数中的this
function countDown(){ var self = this; var doWork = function(){ console.log(this);//window console.l ...
- 如何更好的利用redis
原文地址http://oldblog.antirez.com/post/take-advantage-of-redis-adding-it-to-your-stack.html @(syoka)[re ...
- 基于JUnit和Ant测试程序正在运行使用Kieker(AspectJ)监测方法
这篇日志的目的从标题里能够看出来.这也是我们实验须要,必须总结一下,方便其它师弟师妹在这个基础上做实验. 我已经介绍了非常多基于Kieker的监控方法,这里以Prefuse这个开源可视化Java框架为 ...
- DDD实战8_1 实现对领域中连接字符串的可配置
1.在webapi的配置文件中配置连接字符串节 2.在webapi的startup类中的Configure方法中 将工具类里面AppSetting的静态Section的值 对应上webapi的配置文件 ...
- android网络开源框架volley(五岁以下儿童)——volley一些细节
最近的一次volley整理出下一个.我以前没有再次遭遇了一些小问题,在该记录: 1.HttpUrlConnection DELETE 信息不能加入body问题:java.net.ProtocolExc ...
- WinEdt && LaTex(五)—— 内容的排版
1. 无序列表 需要的环境是\begin{itemize} \end{itemize} \begin{itemize} \item hello \item world \end{itemize} 2. ...
- silverlight,WPF动画终极攻略之阳光灿烂篇(Blend 4开发)
原文:silverlight,WPF动画终极攻略之阳光灿烂篇(Blend 4开发) 前面我们画了一只会飞动的小鸟,今天我们在目标是一个会发光的太阳.本章节的动画虽然简单,但是实现的效果可是一点也不打折 ...
- 算法(algorithm)、模型(model)与框架(framework)
模型对应的数学公式,公式中往往有待学习得到的参数,因此在进行训练或者学习时,首先初始化这部分参数(0 或标准正太分布): 学习之前的初始化:initial model: 学习完成之后的模型:final ...
- NS2网络模拟(6)-homework02.tcl
1: #NS2_有线部分\homework02.tcl 2: 3: #Create a simulator object 4: set ns [new Simulator] 5: 6: #Define ...
- 【书单】matlab 科学计算、数值分析以及数学物理问题
1. 数学计算 MATLAB数值计算 MATLAB之父 : 编程实践 2. 数学物理问题 高等应用数学问题的MATLAB求解(第3版)(豆瓣评价极好) 3. 模式识别