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的更多相关文章

  1. day 84 Vue学习六之axios、vuex、脚手架中组件传值

    Vue学习六之axios.vuex.脚手架中组件传值   本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的 ...

  2. day 87 Vue学习六之axios、vuex、脚手架中组件传值

      本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的使用 Axios 是一个基于 promise 的 HT ...

  3. Vue(5)- axios、vuex

    一.内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loade ...

  4. Vue2基于Axios Ajax Vuex的Loading组件

    1. 定义根state:ajaxIsLoading2. 在Axios拦截器中commit不同的状态实现状态切换3. 组件中通过getter获取ajaxIsLoading状态 Axios 拦截器配置 i ...

  5. Vue 5 -- axios、vuex

    一.内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loade ...

  6. 【实战】Vue全家桶(vue + axios + vue-router + vuex)搭建移动端H5项目

    使用Vue全家桶开发移动端页面. 本博文默认已安装node.js. github链接 一.准备工作 安装vue npm install vue 安装脚手架vue-cli npm install -g ...

  7. [前端] VUE基础 (9) (element-ui、axios、Vuex)

    一.element-ui的使用 官方网页:https://element.eleme.cn/#/zh-CN 1.安装element-ui (venv) D:\pycharm_workspace\vue ...

  8. Vuex与axios介绍

    Vuex集中式状态管理里架构 axios (Ajax) Vuex集中式状态管理架构 -简单介绍: vuex是一个专门为Vue.js设计的集中式状态管理架构. 我们把它理解为在data中需要共享给其他组 ...

  9. vue-x和axios的学习

    axios的使用 使用原因:因为vue本身就带有处理dom的功能,不希望再有其他操作dom的插件,所以用axios代替了jquery 功能:发送xhr请求 下载: $ npm install axio ...

随机推荐

  1. React Native细节记录

    1.环境搭建部分 安装完node后建议设置npm镜像以加速后面的过程(或使用***工具).注意:不要使用cnpm!cnpm安装的模块路径比较奇怪,packager不能正常识别! npm config ...

  2. 手推机器学习公式(一) —— BP 反向传播算法

    方便起见,本文仅以三层的神经网络举例. f(⋅):表示激励函数 xi:表示输入层: yj:表示中间的隐层: yj=f(netj) netj=∑i=0nvijxi ok:表示输出层,dk 则表示期望输出 ...

  3. Arcgis api for javascript学习笔记(4.5版本)-三维地图并叠加天地图标注

    1.三维地图实现 在官网的demo中就有三维地图的实现,如下图所示 <!DOCTYPE html> <html> <head> <meta charset=& ...

  4. WinForm - 窗体淡入效果界面的简单实现方法

    WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...

  5. CAP和最终一致性

    查阅资料整理了最终一致性.CAP 相关的内容.由于图省事儿,没有做文字的整理记载,只有 slides 和一些查阅过的链接,大家将就着看.欢迎指正. slides: slides 链接:请戳这里 背景 ...

  6. 自定义滚动条样式 -webkit-scrollbar

    demo .page-one-content-area-inner-select-wrap height 200px margin-bottom 30px overflow auto &::- ...

  7. Windows,linux下编译qt源码(比较简单)

    一.linux下静态编译qt源码 1.取到qt源码并解压到文件夹 2.cd到qt目录下 3.使用configure生成makefile ./configure–prefix /opt/qtstatic ...

  8. 构建自己的PHP框架(ORM)

    完整项目地址:https://github.com/Evai/Aier 我们选择 Laravel 的 illuminate/database 作为我们的 ORM 包. 在本系列教程里,每一个 Comp ...

  9. 你所不知道的 Kindle - 阅读微信公众号文章

    Kindle 是一款非常优秀的阅读设备,它为我们提供了非常舒服的阅读体验,并且配合强大的亚马逊图书资源,应该是目前最好的阅读设备之一.Kindle 在已有的成就下还一直在努力提升用户体验.为中国用户开 ...

  10. Springboot统一配置Jackson

    经常要为接口响应对象设置属性,序列化的时候是不是包含空值,反序列化的时候是否忽略不认识的字段.所以,必须要手动制定ObjectMapper或者在类上声明 @JsonInclude(Include.NO ...