Angular 2 Web Worker - UI 未运行 [英] Angular 2 Web Worker - UI not running

查看:19
本文介绍了Angular 2 Web Worker - UI 未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got an angular2 application running in a web worker. By the looks of it, everything is running as I would expect, but nothing seems to be happening with the DOM. For example, I show a pre-loader while the app is loading, but it never gets replaced with the actual application UI.

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

当 AppModule 加载时,我的应用程序中没有任何内容......即使我看到我的所有组件和服务都按照我在后台工作程序中的预期运行......整个应用程序运行良好 - 只是没有 UI.

When the AppModule loads, nothing renders in my-app... even though I see all my components and services running exactly as I would expect inside the background worker... the whole app is running fine - just no 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() { }    
}

在我的 main.ts 中

In my main.ts

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 中

In my webworker-main.ts

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

当我不使用网络工作者时,一切都如我所愿.我真的很接近这个工作,我相信......只是找不到我错过的东西.我怀疑有些东西我没有加载 - 或者我不知道与路由相关的东西.

Everything works as I would expect when I don't use the web-worker. I'm really close to having this working I believe... Just can't find what I'm missing. I suspect there is either something I'm not loading - or something I'm not aware of related to routing.

推荐答案

想通了.

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

The issue is that an additional provider must be supplied to bootstrapWorkerUi in main.ts - specifically WORKER_UI_LOCATION_PROVIDERS from @angular/platform-webworker.

新的(工作)main.ts

The new (working) 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;
    });
});

这篇关于Angular 2 Web Worker - UI 未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆