如何在Nuxt.js组件中服务器端呈现HTTP请求

时间:2019-09-26 01:29:40

标签: javascript vue.js vue-component nuxt.js

我想在服务器端的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;
};

0 个答案:

没有答案