我想在服务器端的Nuxt.js组件中检索一些数据,以便可以将其呈现并发送给客户端。
我尝试使用https://github.com/nuxt/nuxt.js/issues/32中的fetchDeep
,但我不知道如何使它在所有组件上运行。
function fetchDeep(component){
var originFn =component.asyncData;
component.asyncData =async function (ctx) {
if(component.components){
let childComponents =Object.values(component.components);
console.log(childComponents);
while(childComponents.length){
let comp =childComponents.shift();
if(comp.options.fetch) await comp.options.fetch(ctx);
if(comp.components) childComponents.push(...Object.values(comp.components));
}
};
return originFn(ctx);
};
return component;
};