0.babel

将es6代码转换成各个浏览器都能识别的代码

一.axios

1.官方网站

https://www.kancloud.cn/yunye/axios/234845

2.引用:

(1)cdn

  1. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

(2)下载到当前项目***

  1. npm install axios

3.用法

(1)get请求

  1. // 为给定 ID 的 user 创建请求
  2. axios.get('/user?ID=12345')
  3. .then(function (response) {
  4. console.log(response);
  5. })
  6. .catch(function (error) {
  7. console.log(error);
  8. });
  9.  
  10. // 可选地,上面的请求可以这样做
  11. axios.get('/user', {
  12. params: {
  13. ID: 12345
  14. }
  15. })
  16. .then(function (response) {
  17. console.log(response);
  18. })
  19. .catch(function (error) {
  20. console.log(error);
  21. });

(2)post请求

  1. axios.post('/user', {  
  2. firstName: 'Fred',  
  3. lastName: 'Flintstone' })
  4. .then(function (response) {  
  5. console.log(response);
  6. })
  7. .catch(function (error) {  
  8. console.log(error);
  9. });

4.使用实例

在main.js中,将Axios挂载到 Vue原型上,每一个子组件都能够使用

  1. import Axios from 'axios'
  2.  
  3. Vue.prototype.$https = Axios
  4.  
  5. Axios.defaults.baseURL = 'https://www.luffycity.com/api/v1/';

二.vuex

作用:组件之间的传值,任何一个组件都能共享state中的数据

下载vuex到该组件:npm i vuex -S

1.vuex商店的创建

在main.js中

  1. import Vue from 'vue'
  2. import App from './App'
  3. import router from './router/index'
  4.  
  5. //1.引入
  6. import Vuex from "vuex"
  7. //2.组件使用要use
  8. Vue.use(Vuex)
  9. //3.创建一个store实例
  10. const store=new Vuex.Store({
  11. state:{},
  12. mutations:{},
  13. actions:{},
  14. })
  15.  
  16. new Vue({
  17. el: '#app',
  18. router,
  19. //4.挂载store实例
  20. store,
  21. components: { App},
  22. template: '<div><App></App></div>'
  23. })

2.值的使用

  1. <template>
  2. <div class="">
  3. //2.使用
  4. <h3>我是主页,我是父组件中的{{ num }}</h3>
  5.  
  6. </div>
  7. </template>
  8.  
  9. <script>
  10. export default {
  11. name: 'Home',
  12. data () {
  13. return {
  14.  
  15. }
  16. },
  17. //监听
  18. computed:{
  19. //1.定义方法,调用store,返回store中的值
  20. num:function(){
  21. return this.$store.state.num
  22. }
  23. }
  24. }
  25. </script>
  26.  
  27. <style >
  28.  
  29. </style>

3.值的同步修改

在main.js

  1. import Vue from 'vue'
  2. import App from './App'
  3. import router from './router/index'
  4.  
  5. // Vue.config.productionTip = false
  6.  
  7. import Vuex from "vuex"
  8. Vue.use(Vuex)
  9. const store=new Vuex.Store({
  10. state:{num:111},
  11. mutations:{
  12. //1.定义如何修改
  13. setNum(state,val){
  14. state.num+=val
  15. }
  16. },
  17. actions:{},
  18. });
  19.  
  20. new Vue({
  21. el: '#app',
  22. router,
  23. //4.挂载store实例
  24. store,
  25. components: { App},
  26. template: '<div><App></App></div>'
  27. });

在Course.vue中

  1. <template>
  2. <div>我是课程
  3. <button @click="setnum">+1按钮</button>
  4. 我是父组件中的{{ num }}
  5. </div>
  6. </template>
  7. <script>
  8. export default{
  9. methods:{
  10. //2.确定修改
  11. setnum(){
  12. this.$store.commit("setNum",1)
  13. }
  14. },
  15. computed:{
  16. num:function(){
  17. return this.$store.state.num
  18. }
  19. }
  20. }
  21. </script>
  22. <style></style>

4.值的异步修改

在main.js中

  1. import Vue from 'vue'
  2. import App from './App'
  3. import router from './router/index'
  4.  
  5. import Vuex from "vuex"
  6. Vue.use(Vuex)
  7. const store=new Vuex.Store({
  8. state:{num:111},
  9. mutations:{
  10. setMutaNum(state,val){
  11. state.num+=val
  12. },
  13. setMutaAsync:function(state,val){
  14. state.num+=val
  15. }
  16. },
  17. actions:{
  18. setActionNum(context,val){
  19. context.commit('setMutaNum',val)
  20. },
  21. setActionAsync(context,val){
  22. setTimeout(()=>{
  23. context.commit('setMutaAsync',val)
  24. })
  25. }
  26. },
  27. });
  28.  
  29. new Vue({
  30. el: '#app',
  31. router,
  32. //4.挂载store实例
  33. store,
  34. components: { App},
  35. template: '<div><App></App></div>'
  36. });

在course.vue中

  1. <template>
  2. <div>我是课程
  3. <button @click="setnum">同步+1按钮</button>
  4. <button @click="setAsynanum">异步 +5按钮</button>
  5. 我是父组件中的{{ num }}
  6. </div>
  7. </template>
  8. <script>
  9. export default{
  10. methods:{
  11. setnum(){
  12. this.$store.dispatch("setActionNum",1)
  13. },
  14. setAsynanum(){
  15. this.$store.dispatch("setActionAsync",5)
  16. }
  17. },
  18. computed:{
  19. num:function(){
  20. return this.$store.state.num
  21. }
  22. }
  23. }
  24. </script>
  25. <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. xshell/putty 连接 linux 虚拟机 connection failed 的解决方案

    ubuntu 默认并没有安装 ssh 服务,如果通过 ssh(XShell/putty) 连接 ubuntu 虚拟机,则需要手动安装 ssh-server(ssh 分客户端和 openssh-clie ...

  2. three.js 初学小示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Docker 入门基础

    Docker是一个能把开发的应用程序自动部署到容器的开源引擎.Docker是新的容器化技术,轻巧,易移植[Build Once, Configure Once And Run Anywhere].Do ...

  4. mysql安装出现应用程序无法正常启动(oxc000007b)的解决方案

    原文:mysql安装出现应用程序无法正常启动(oxc000007b)的解决方案 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/IUNIQUE/art ...

  5. 获取当前电脑的cpu使用率、内存使用率

    https://www.cnblogs.com/Chary/p/7771365.html http://www.cnblogs.com/zl1991/p/4679461.html 要关注几个类 Per ...

  6. java中的switch结构

     switchkeyword的中文意思是开关.转换的意思,switch语句在条件语句中特别适合做一组变量相等的推断,在结构上比if语句要清晰非常多.switch语句的语法格式为:switch(表达式) ...

  7. python 教程 第十六章、 正则表达式

    第十六章. 正则表达式 1)    匹配多个表达式 记号  re1|re2 说明  匹配正则表达式re1或re2 举例  foo|bar  匹配  foo, bar 记号  {N} 说明  匹配前面出 ...

  8. 嵌入式Linux开发环境的搭建

    一个.制造u-boot.bin文件:    tar xjf u-boot-1.1.6.tar.bz2    cd u-boot-1.1.6    patch -p1 < ../u-boot-1. ...

  9. 许多其他C++的class样本

    class A{  public:  A(){}//构造函数,作用分配类所需的空间 }; int main() {  A a; } a它是类A示例! 版权声明:本文博客原创文章.博客,未经同意,不得转 ...

  10. WPF依赖项属性不需要包装属性也可以工作

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...