Nuxt.js asyncData 发送请求
Nuxt.js asyncData 发送请求
单次
<template>
<div>
<h1>asyncData</h1>
{{name}}
{{info}}
{{info.name}}
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
name:'Hellowowd',
}
},
asyncData() {
return axios.get('/json.json').then(res=>{
return {info:res.data}
})
},
// async asyncData(){
// let {data}=await axios.get('')
// return {info:data}
// }
}
</script>
多次
<template>
<div>
<h1>asyncData</h1>
{{name}}
<p>info1:{{info1}}</p>
<p>info2:{{info2}}</p>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
name:'Hellowowd',
}
},
async asyncData () {
let [res1, res2] = await Promise.all([
axios.get('/json.json'),
axios.get('/json1.json'),
])
return {
info1:res1.data,
info2: res2.data,
}
}
}
</script>
json文件在static文件夹下