首页 > 解决方案 > 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...

标签: vue.jsaxiosnuxt.js

解决方案


如果您想要一个 SSR 页面,则可以在页面内的 asyncData 方法中发出 API 请求,或者如果您想在客户端发出 API 请求,则可以使用 mount 方法。

而对于状态管理/管理组件中/跨组件的数据,可以通过VueX来实现


推荐阅读