vue.js - How to call REST api in NUXT efficiently? (also at component based frontend frameworks)
问题描述
I want to get my data from server in the component where the data is needed.
(call api at mounted
or beforemounted
or created
(component lifecycle loop))
If I followed the rule above, it is inevitable to call same multiple REST api at same page.
I tried to cache them but it is quite difficult to handle the REST api result. ( Because the result of REST api depends on to params and it also depends on to time....)
I know that nuxt provide fetch and async but getting all data at page level looks bad.
Is there any good strategy for call api efficiently? I think this kinds of concern is not restricted to nuxt or vue, it also happens in react, angualr like component based framework...
解决方案
如果您想要一个 SSR 页面,则可以在页面内的 asyncData 方法中发出 API 请求,或者如果您想在客户端发出 API 请求,则可以使用 mount 方法。
而对于状态管理/管理组件中/跨组件的数据,可以通过VueX来实现
推荐阅读
- django - 如何在 Django 中将用户发送的数据关联到他自己的帐户
- reactjs - 如何在 Monday.com 中使用列值创建新的板项目?代码不工作
- jquery - 无法从 Parsley AJAX 输入值验证器获得响应
- node.js - “属性或方法“产品”未在实例上定义,但在渲染期间引用“Vue App 中的错误
- azure-container-instances - ACI容器组退出成功后消失
- arrays - 例外:Invalid_argument“索引超出范围”
- r-markdown - 在 Rmarkdown HTML 输出中插入自定义间隔的分页符
- css - 简单字体颜色 CSS 中的各种错误代码
- flutter - 在 Flutter 中检查 BLE 是否关闭时显示底页
- shell - 如何将makefile函数的输出分配给变量