NUXT中使用axios,自己项目测试记录
<template>
<h4>
data = {{ res }}
</h4>
</template>
<script>
import axios from 'axios'
export default {
name: 'four',
// 第一种方法
/*
async asyncData (context) {
// const { data } = await axios.get('http://yd.ysedu.com/app/v1/get_home_countdown_details')
const { data } = await axios({
method: 'get',
url: 'http://yd.ysedu.com/app/v1/get_home_countdown_details',
data: {
id: 5
},
headers: {
sign: '6ebbd40d0baeef0417bf6b62144f05e3/22307e52eb1895b809031eba30900f80/1578654048/m'
}
})
return { res: data }
}
*/
// 方法2
async asyncData (context, callback) {
axios({
method: 'get',
url: 'http://yd.ysedu.com/app/v1/get_home_countdown_details',
data: {
id: 5
},
headers: {
sign: 'cf1b832265a99354032d8ced757f1e77/22307e52eb1895b809031eba30900f80/1578654419/m'
}
}).then(
res => {
callback(null, {res: res.data})
}
)
}
}
</script>
<style scoped>
</style>
初学,测试写不好返回结果,经过几次测试终于稳定。

浙公网安备 33010602011771号