1.安装依赖

  1. yarn add axios

2.utils/config.js中导入axios

  1. import axios from 'axios' //导入
  2. axios.defaults.baseURL='http://192.168.14.49:5000' //主地址
  3. Vue.prototype.axios=axios;

3.对应的文件index.vue中发送请求

  1. import ItemIndex from '@/components/ItemIndex' //导入组件
  2. export default {
  3. name:"Music",
  4. data(){
  5. return{
  6. playlists:[]
  7. }
  8. },
  9. //注册组件
  10. components:{
  11. ItemIndex
  12. },
  13. mounted(){
  14. this.axios.get("/top/playlist?cat=华语&limit=12").then(res=>{
  15. this.playlists=res.data.playlists
  16. })
  17. }
  18. }
  19. //4.渲染数据
  20. <template>
  21. <div class="container">
  22. <item-index :data="item" v-for="item of playlists" :key="item.id">
  23. <div slot="icon" class="icon">
  24. <img src="../../assets/images/p0.png" alt="">
  25. {{item.playCount}}
  26. </div>
  27. </item-index>
  28. </div>
  29. </template>

1-6课件地址https://gitee.com/wangsiman/notebooks/tree/master/vue/07rem-router