1.父组件发送请求将取到的数组传到子组件

  1. <template>
  2. <div>
  3. <Hot :data="playlists" ></Hot> //通过自定义传值的方式
  4. </div>
  5. </template>
  6. <script>
  7. import Hot from '@/components/Hot.vue'
  8. export default {
  9. name:"Music",
  10. data(){
  11. return{
  12. playlists:[],
  13. }
  14. },
  15. components:{
  16. Hot,
  17. },
  18. mounted(){
  19. var url="https://music.aityp.com/top/playlist";
  20. this.axios.get(url).then(res=>{
  21. this.playlists=res.data.playlists.slice(0,3);
  22. })
  23. }
  24. }
  25. </script>

2.子组件接收传过来的数组并遍历

  1. <template>
  2. <div v-for="item of data" :key="item.id">
  3. <div class="list">
  4. <img class="pic" :src="item.coverImgUrl" alt="" />
  5. <p>{{item.name}}</p>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. data: {
  13. type: Array
  14. }
  15. }
  16. };
  17. </script>