12、Vue之Axios fetchJsonp请求数据(第三方模块)

数据请求步骤(与vue-resource略有不同):
一、axios的配置:
1、在相应的工程中 npm install axios --save(‘save’的作用是将模块保存在package.json中,以便项目转接时省事);


image.png

2、哪里用,那里引入即可:
import Axios from 'axios'


image.png

二、使用:
1、在组件中使用:(以QQ音乐接口为例)
let api='https://api.bzqll.com/music/tencent/songList?key=579621905&id=1147906982';
Axios.get(api).then((response)=>{
console.log(response)
}).catch((err)=>{
console.log(err)
})

image.png

代码:
Home.vue:


image.png



<template>

<div>
<h2>这是一个首页组件</h2>
<button @click="getData()">请求数据</button>
</div>
</template>


<script>
import Axios from 'axios'//哪里用,就在那里引用(比如,Home.vue这个组件里要用,则在此处引入)
export default {
name: "home",
data(){
return {
msg:'我是一个首页',
list:[],
}
},
methods:{
getData(){
let api='https://api.bzqll.com/music/tencent/songList?key=579621905&id=1147906982';
Axios.get(api).then((response)=>{
console.log(response)
}).catch((err)=>{
console.log(err)
})
}
},
}
</script>



<style scoped lang="scss">
h2{
color:red;
}
</style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数据请求步骤:一、vue-resource的配置:1、在相应的工程中(注意:一定要在相应的项目文件夹中) npm ...
    msqt阅读 526评论 1 3
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,735评论 0 3
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,088评论 0 29
  • _________________________________________________________...
    fastwe阅读 1,412评论 0 0
  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 3,018评论 1 4