全部代码
<template>
<div>
<!-- 9.如果请求到的data为true 则让它显示到页面-->
<template v-if="data">
<!-- 10.先整体一个item的div,以此为基础遍历data的数据-->
<div class="item" v-for="ar in data.subjects" :key="ar.id">
<!-- 11.整体的div里面包裹的div -->
<img :src="ar.cover" alt="" />
<!-- 12.整体div下的div,并包裹两个span标签 -->
<div>
<!-- 如果拿到的数据is_new是false 则不显示新这个字 -->
<span class="new" v-if="ar.is_new">新</span>
<span>{{ ar.title }}</span>
<span>{{ ar.rate }}</span>
</div>
</div>
</template>
</div>
</template>
<script>
export default {
data() {
return {
data: null
}
},
mounted() {
console.log(this)
this.getData()
},
methods: {
getData() {
const url = 'http://api.xin88.top/douban/movies.json'
this.axios.get(url).then((res) => {
console.log(res)
this.data = res.data
})
}
}
}
</script>
<style lang="scss" scoped>
.item {
width: 200px;
margin: 10px;
display: inline-block;
> img {
width: 100%;
}
> div {
display: flex;
justify-content: center;
> .new {
padding: 5px;
background-color: rgb(85, 201, 153);
}
}
}
</style>
请求成功的结果
要提前准备的环境
- vue的项目包,需在项目部路径npm run serve启动
- axios的安装.在项目包路劲cmd输入命令npm i axios vue-axios安装