1.下载axios
npm install @nuxtjs/axios -S
2.在nuxt.config.js中引入
modules: [
'@nuxtjs/proxy',
'@nuxtjs/axios'
],
3. api解耦
3.1 配置: nuxt.config.js
plugins: [
'~/plugins/axios',
'~/api/xxx.js'
],
3.2 xxx.js中写入
export default ({$axios},inject)=>{
inject('getFirstCategorys',()=>$axios({
url:'/api/course/category/getFirstCategorys',
method:'GET',
}))
}
3.3 页面或者组件请求
async asyncData( app ){
let res = await app.$getFirstCategorys();
return {
list:res.data.list
}
}
相关作者
- 获取点赞0
- 文章阅读量133
评论()