Angular 2 Web Worker - UI未运行

时间:2016-11-26 02:21:05

标签: angular web-worker

我在网络工作者中运行了一个angular2应用程序。从它的外观来看,一切都在按照我的预期运行,但DOM似乎没有发生任何事情。例如,我在应用程序加载时显示预加载器,但它永远不会被实际的应用程序UI替换。

<my-app>Loading...</my-app>

当AppModule加载时,我的应用程序中没有任何渲染...即使我看到我的所有组件和服务运行完全如我在背景工作者中所期望的那样...整个应用程序运行正常 - 只是没有UI。

import { NgModule } from '@angular/core';
import { HttpModule, XSRFStrategy } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { WorkerAppModule } from '@angular/platform-webworker';
import { WORKER_APP_LOCATION_PROVIDERS } from '@angular/platform-webworker';

import { AppComponent } from './app.component';
import { NoXSRFStrategy } from './common/backends/XSRFStrategies';

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        FormsModule,
        routing,  

        WorkerAppModule         
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        { provide: XSRFStrategy, useClass: NoXSRFStrategy },

        WORKER_APP_LOCATION_PROVIDERS           
    ],
    bootstrap: [AppComponent]
})
export class AppModule {    
    constructor() { }    
}

在我的主页

UI_THREAD_CHANNEL = 'UI_THREAD_CHANNEL'
bootstrapWorkerUi('/webworker-loader.js').then((hWnd) => {

                // register methods the WebWorker needs to run on the UI thread.
                let brokerFactory: ServiceMessageBrokerFactory = hWnd.injector.get(ServiceMessageBrokerFactory);
                let broker = brokerFactory.createMessageBroker(UI_THREAD_CHANNEL, false);

                broker.registerMethod('redirect', [PRIMITIVE], (href: string) => {
                    window.location.href = href;
                });
            });

在我的webworker-main.ts

import { platformWorkerAppDynamic } from '@angular/platform-webworker-dynamic';
import { AppModule } from './app.module';
const platform = platformWorkerAppDynamic();
platform.bootstrapModule(AppModule);

当我不使用网络工作者时,一切都按照我的预期运作。我真的很接近这项工作我相信......只是找不到我失踪的东西。我怀疑有什么东西我没有加载 - 或者我不知道与路由有关的东西。

1 个答案:

答案 0 :(得分:2)

想出来。

问题是必须在main.ts中为bootstrapWorkerUi提供额外的提供程序 - 特别是来自@ angular / platform-webworker的WORKER_UI_LOCATION_PROVIDERS。

新的(工作)main.ts

import { WORKER_UI_LOCATION_PROVIDERS, bootstrapWorkerUi, 
ServiceMessageBrokerFactory } from '@angular/platform-webworker';

UI_THREAD_CHANNEL = 'UI_THREAD_CHANNEL'
bootstrapWorkerUi('/webworker-loader.js', WORKER_UI_LOCATION_PROVIDERS).then((hWnd) => {

    // register methods the WebWorker needs to run on the UI thread.
    let brokerFactory: ServiceMessageBrokerFactory = hWnd.injector.get(ServiceMessageBrokerFactory);
    let broker = brokerFactory.createMessageBroker(UI_THREAD_CHANNEL, false);

    broker.registerMethod('redirect', [PRIMITIVE], (href: string) => {
        window.location.href = href;
    });
});