Angular 2 网络工作者 - UI 不是 运行
Angular 2 Web Worker - UI not running
我在 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() { }
}
在我的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
import { platformWorkerAppDynamic } from '@angular/platform-webworker-dynamic';
import { AppModule } from './app.module';
const platform = platformWorkerAppDynamic();
platform.bootstrapModule(AppModule);
当我不使用 web-worker 时,一切正常。我真的很接近让这项工作我相信......只是找不到我所缺少的东西。我怀疑有些东西我没有加载 - 或者我不知道与路由相关的东西。
想通了。
问题是必须向 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;
});
});
我在 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() { }
}
在我的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
import { platformWorkerAppDynamic } from '@angular/platform-webworker-dynamic';
import { AppModule } from './app.module';
const platform = platformWorkerAppDynamic();
platform.bootstrapModule(AppModule);
当我不使用 web-worker 时,一切正常。我真的很接近让这项工作我相信......只是找不到我所缺少的东西。我怀疑有些东西我没有加载 - 或者我不知道与路由相关的东西。
想通了。
问题是必须向 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;
});
});