我现在正在使用Vue和Nuxt框架来编写网站。 将API连接到我的网站后, 它在一开始就起作用了,但是突然间一切都崩溃了:
p.s。根据vscode终端控制台日志显示,我可以完美地获取API数据,但是当我在Chrome上进行测试时,没有任何显示。
无法弄清楚问题出在哪里,因为有点奇怪
我们使用新设置来获取API
我们如何获得API:
import axios from 'axios'
const packageFile = require('../package.json')
const client = axios.create({
baseURL: packageFile.APIUrl,
json: true
})
function api(method, resource, data) {
return client({
method,
url: resource,
headers: {},
data
})
.then(req => {
console.log('axios setting successed')
return req.data
})
.catch(err => {
console.log('axios setting failed')
console.log(err)
})
}
export { api }
import { api } from './AxiosSetting'
export default {
async getEventInfo() {
const res = await api('get', 'api/Events/get-latestevent')
return res
}
}
<template v-if="eventPackage.length>0">
<figure class="box-pic">
<img :src="eventPackage[0].reviewImg" alt>
</figure>
<div class="figure-caption">
<h4>{{eventPackage[0].eventName}}</h4>
<p>{{eventPackage[0].eventDate}}</p>
<a :href="eventPackage[0].link" class="btn-more">Read more</a>
</div>
</template>
..
..
..
<script>
import GetEvent from '../service/GetEvent.js'
export default {
name: 'EcsEvent',
data() {
return {
eventPackage: []
}
},
created() {
this.pushEventPackage()
console.log(this.eventPackage) //works
},
mounted() {
this.pushEventPackage()
console.log(this.eventPackage) //doesn't work
},
methods: {
pushEventPackage() {
GetEvent.getEventInfo().then(res => {
console.log(res)
this.eventPackage = res
})
}
}
}
</script>