API调用后bootstraping angular4 app AppModule可能吗?

时间:2018-02-15 06:34:49

标签: angular bootstrapping

我想在启动/初始化angular4 AppModule之前调用API。这有可能吗?

import * as mongoose from "mongoose";

(<any>mongoose).Promise = global.Promise;
let cacheDb: mongoose.Connection = null;
export let connect = () => {
    if (cacheDb && mongoose.connection.readyState === 1) {
        return cacheDb;
    } else {
        // Mongoose config and setting global Promise
        mongoose.connect(process.env.MONGODB_URI, {
            promiseLibrary: global.Promise,
            connectTimeoutMS: 20 * 1000
        }).then(() => {
            cacheDb = mongoose.connection;
            return cacheDb;
        }).catch((err) => {
            console.log(err);
        });
    }
};
export let disconnect = () => {
    mongoose.disconnect();
}

在初始化bootstrap组件'AppComponent'之前,我想调用一个API。

1 个答案:

答案 0 :(得分:1)

您可以使用APP_INITIALIZER在bootstrap上调用服务方法。您需要在AppModule中为其定义提供程序。

您可以参考此question