查看“︁Node.js/nuxt/axios”︁的源代码
来自康健生活
←
Node.js/nuxt/axios
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == == 复杂数据格式 == post 请求中常见的 content-type 的值有四种 * application/x-www-form-urlencoded 表单提交的数据类型 * multipart/form-data 文件上传时要使用的数据类型 * application/json json 格式的数据类型(默认类型) * text/xml {{Warning|axios 默认的数据类型为 application/json,如果后端不能接受到 post 的请求的信息体,就是因为不能正确识别 json 请求}} == 封装 == <blockquote><pre> @nuxtjs/axios</pre></blockquote> nuxt 自带 @nuxtjs/axios,不需单独安装 axios。 在页面组件中引用时,在 mounted 中有效,在 methods 中无效。 <syntaxhighlight lang="javascript"> <script> export default { name: "ly-footer", ... mounted() { this.$axios.get.....; }, methods: { ... }, } </script></syntaxhighlight> 需要注意的是 @nuxtjs/axios 不支持 axios.all 方法。(使用 Promise.all 代替) <syntaxhighlight lang="javascript"> // In asyncData or fetch // Parallel requests const [ foo, bar] = await Promise.all([getFoo(), getBar()]) // Sequential requests const foo= await getFoo() const bar= await getBar()</syntaxhighlight> {{leftrightflex|<syntaxhighlight lang="javascript"> axios.all([ axios.get('/v2/disease/footer_recommend'), axios.get('/v2/hospital/recommend/rand') ]).then(axios.spread(function( Disease_footer_recommend, HospitalRecommendRand ) { _self.Disease_footer_recommend = Disease_footer_recommend.data.data console.log(HospitalRecommendRand) _self.HospitalRecommendRand = HospitalRecommendRand.data.data })).catch((error) => { console.log(error) _self.errorMessage = error }) </syntaxhighlight>|{{consoleout|<pre> Promise.all([ axios.get('/v2/disease/footer_recommend'), axios.get('/v2/hospital/recommend/rand') ]).then(([ Disease_footer_recommend, HospitalRecommendRand]) => { return { Disease_footer_recommend: Disease_footer_recommend.data.data, HospitalRecommendRand: HospitalRecommendRand.data.data } })</pre>}}}} 或者自己封装 <syntaxhighlight lang="javascript"> // Expose all/spread axios.all = function all(promises) { return Promise.all(promises); }; // Axios.speard module.exports = function spread(callback) { return function wrap(arr) { return callback.apply(null, arr); } };</syntaxhighlight>
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
模板:Meta box
(
查看源代码
)
模板:Warning
(
查看源代码
)
模板:Warning/style.css
(
查看源代码
)
返回
Node.js/nuxt/axios
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。