四、Vue CLI-异步请求(axios)
一、模块的安装
npm install axios --save
#--save可以不用写
如图:

二、配置main.js
import axios from 'axios'
Vue.prototype.$axios = axios;
如图:

三、简单使用示例
页面加载时执行:
<template>
<div class="header">{{title}}</div>
</template> <script>
//当前组件的脚本
export default{
// 当前组件的名称
name:"test", //非必填项,则自动以文件名为主
data(){
return {//当前组件中所有的数据 title:'猫眼电影' };
},methods:{ //当前组件中所有的函数 //普通的方法
xx:function(){
alert("调用普通方法");
} },created()
{
window.console.log("页面加载完毕之前执行,执行顺序:父组件-子组件");
this.xx(); },mounted()
{
window.console.log("页面加载完毕之前执行,执行顺序:子组件-父组件"); }
}
</script> <style>
.header{
height: 1.25rem;
line-height: 2.5;
background: #e54847;
color: #fff;
text-align: center;
font-size: 0.5rem;
}
</style>
点击按钮执行:
<template>
<div>
<div v-show="show">HelloWorld</div>
<button @click="handleClick">Click</button>
<ul>
<li v-for="(item,index) of list" :key="index">{{item}}</li>
</ul>
</div>
</template>
<script>
export default {
name: "test",
data() {
return {
show: true,
list: [1, 2, 3]
};
},
methods: {
handleClick: function() {
this.show = !this.show;
}
}
};
</script>
异步请求的

代码如下:
<template>
<div>
<div v-show="show">HelloWorld</div>
<button @click="handleClick">Click</button>
<ul>
<li v-for="(item,index) of list" :key="index">{{item}}</li>
</ul>
</div>
</template>
<script>
export default {
name: "test",
data() {
return {
show: true,
list: [1, 2, 3]
};
},
methods: {
handleClick: function() { this.$axios({
url: 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100', // 后台接口
method: 'get', // 请求方式
}).then(response => { // 请求成功
window.console.log('请求成功');
window.console.log(response.data);
this.course_ctx = response.data; // 将后台数据赋值给前台变量完成页面渲染
}).catch(error => { // 请求失败
window.console.log('请求失败');
window.console.log(error);
}) }
}
};
</script>
四、路由跳转与参数
可以使用 this.$router.push(location) 来更改url,完成跳转

代码如下:
接受页面:
<template>
<div>
<div>Test2页面</div>
<div>{{this.$route.params.user}}</div>
<button @click="write">打印接收的参数</button>
</div>
</template>
<script>
export default {
name:"test2",
data(){
return {
status:true
}
},
methods:{
write(){
window.console.log(this.$route.params.user);
}
}
}
</script>
<style>
</style>
点击跳转页面
<template>
<div>
<div v-show="show">HelloWorld</div>
<button @click="handleClick">Click</button>
<ul>
<li v-for="(item,index) of list" :key="index">{{item}}</li>
</ul>
</div>
</template>
<script>
export default {
name: "test",
data() {
return {
show: true,
list: [1, 2, 3]
};
},
methods: {
handleClick: function() { //this.$router.push('/test2') this.$router.push('/test2?user="wise"') //接受页面 this.$route.query.user /*this.$router.push({
name:'test2',
params:{
user:"wise"
}
})*/ }
}
};
</script>
四、Vue CLI-异步请求(axios)的更多相关文章
- vue处理异步请求
vue 处理异步请求 项目中需要 先调一个接口去取到人员编号,再去调另一个借口,人员编号作为参数才能去请求数据 用setTimeout 其实也可以,先new了一个promise对象 ,把请求放在里面, ...
- vue中异步请求渲染问题(swiper不轮播)(在开发过程中遇到过什么问题、踩过的坑)
问题描述: 用vue封装一个swiper组件的时候,发现轮播图不能轮播了. 原因: 异步请求的时间远大于生命周期执行的时间,mounted初始化DOM时数据未返回,渲染数据是空数组,导致轮播图的容器层 ...
- vue发送ajx请求 axios
一. 简介 1.vue本身不支持发送AJAX请求,需要使用vue-resource(vue1.0版本).axios(vue2.0版本)等插件实现 2.axios是一个基于Promise的HTTP请求客 ...
- 在双重for循环内部使用async异步请求axios中遇到的问题
在methods中的方法 async getPro () { let _this = this let newArr = [] await axios.get(`api/v1/dailyProTbms ...
- 记一次vue 普通异步请求微信二进制二维码 乱码 问题解决然后渲染
后端压力大,前端分忧. /*用微信小程序token拿二维码*/ async fetchMINIQRcode({commit,state},params){ var instance = axios.c ...
- 09.1.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回数组
09.1html里 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- 09.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回字符串
cmd下安装axios npm install axios 安装好后,会多出node_modules文件夹 思路: 监听data里的word改变时,发送ajax异步请求数据, 把返回的数据赋值给dat ...
- SpringBoot中异步请求和异步调用(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
- Vue+axios的四种异步请求,参数的携带以及接收
Vue中axios发送GET, POST, DELETE, PUT四种异步请求,参数携带和接收问题 web.xml配置如下 1.GET请求 发送GET请求: <!--params是关键字,说明所 ...
随机推荐
- jquery实现对象数组 筛选出每条记录中的特定属性字段 及根据某个属性值筛选出指定的元素
jquery实现对象数组 筛选出每条记录中的特定属性字段 直接上图: 源码: /** * 对后端返回的数据,筛选出符合报表的列项,多余的列项去除 */ function filterParams(da ...
- leetcode-easy-listnode-141 Linked List Cycle
mycode 98.22% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x ...
- python MySQLdb 一个连接connection多个cursor
使用MySQLdb时,如果创建一个连接connection,多个线程同时使用,会不会有问题? 在下文中,我们将模拟这种场景,看是否会出现问题. 1.示例 1.1 正常的情况 创建一个连接,两个线程同时 ...
- python - yeild
带有yield的函数不仅仅只用于for循环中,而且可用于某个函数的参数,只要这个函数的参数允许迭代参数.比如array.extend函数,它的原型是array.extend(iterable). 带有 ...
- MQTT主题Topic讲解
文章转载于https://www.cnblogs.com/hayasi/p/7792191.html 我们已经把相关的连接报文搞定了.笔者想来想去还是决定先讲解一下订阅报文(SUBSCRIBE ).如 ...
- linux创建软链接
linux软链接linux下的软链接类似于windows下的快捷方式实例:ln -s /home/gamestat /gamestatln -s a b 中的 a 就是源文件,b是链接文件名,其 ...
- zabbix图形刷新延迟解决
环境: 服务端 ip :192.168.1.204 hostname:www.test.com 服务端 ip :192.168.1.206 hostname:www ...
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- CTF—攻防练习之FTP服务后门
主机:192.168.32.152 靶机:192.168.32.156 首先查看靶机开放的端口: nmap 192.168.32.156 nmap -T4 -A -v 192.168.32.156 ...
- python学习之数据类型(dic)
3.8 字典 3.8.1 字典的介绍 字典(dict)是python中唯一的一个映射类型,它是以{ }括起来的键值对组成,在dict中key是唯一的.在保存的时候,根据key来计算出一个内存地址, ...