APP_INITIALIZER在角5中不起作用

问题描述 投票:10回答:1

我需要从Angular初始级别的服务中加载配置。因此,对于演示,我创建了boot moduleboot class并将Angular核心的APP_INITIALIZER加载到启动模块中。

检查以下文件,我看不到console.log(“ Init Function works”))以及console.log(“ GET CONFIG WORKS”]]]]

如果有任何问题,请纠正我。

//模块文件boot.modue.ts

import { APP_INITIALIZER, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Boot, Config, Menu } from './../../core/classes';
import { MenusService } from './../services/';

export function init(_boot: Boot) {
  console.log("Init Function works");
  return () => {
    return _boot.getConfig();
  };
}


@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [],
  providers: [Boot,
    { provide: APP_INITIALIZER, 
      useFactory: init, 
      deps: [Boot], multi: true 
    }]
})

export class BootModule {
  constructor() { }
}

//类文件boot.ts

import 'rxjs/add/operator/toPromise';

export class Boot {
    /**
     * getConfig
     */
    public getConfig(): Promise<any> {
        console.log("getConfig");
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log("GET CONFIG WORKS");
                resolve();
            }, 3000);
        });
    }

    /**
     * initMenus
     */
    public initMenus() {

    }
}

我需要从Angular初始级别的服务中加载配置。因此,在演示中,我创建了启动模块,启动类,并将Angular核心中的APP_INITIALIZER加载到启动模块中。检查...

angular typescript angular5 es6-promise
1个回答
0
投票

它们的问题是您的init_app函数,它应该返回Promise,但是您用花括号将其弄乱了,然后返回void。

© www.soinside.com 2019 - 2024. All rights reserved.