https://developers.douban.com/wiki/?title=api_v2
这里选电影作为参考
Axios解决跨域访问
1、打开config>>config/index.js>>index.js找到里面的dev:
2、打开router>>main.js进行如下配置,就可以进行访问了:
3、打开HelloWorld.vue,写以下代码:
<template>
<div class="hello">
<div v-for="movie in movies">
<p>{{movie.title}}</p>
<img :src="movie.img" alt=""/>
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Axios 解决跨域问题 访问api豆瓣电影',
movies:[]
}
},
created() {
let url = this.HOST + '/movie/top/250';
this.$axios.get(url,{
params: {
count: 10,
start: 0
}
})
//拿到数据后进行读取
.then(res => {
this.filterData(res.data);
})
//捕获错误
.catch(error => {
console.log(error);
})
},
//在模板中显示出来
methods: {
filterData(data) {
var finalMovies = [];
for (var i = 0;i < data.subject.length;i++) {
var moviesObj = {
title:data.subjects[i].title,
img:data.subjects[i].images.large
}
finalMovies.push(moviesObj);
}
this.movies = finalMovies;
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>