【发布时间】:2018-08-14 08:43:34
【问题描述】:
我尝试在 web worker 中运行我的 angular6 web 应用程序,如 here 所述
由于 angular6 中缺少 ng eject,我使用 this 创建自定义 webpack 配置文件。
workerLoader.ts
import './polyfills.ts';
import '@angular/core';
import '@angular/common';
import { platformWorkerAppDynamic } from '../node_modules/@angular/platform-webworker-dynamic';
import { AppModule } from './app/app.module';
platformWorkerAppDynamic().bootstrapModule(AppModule);
main.ts
import { enableProdMode } from '@angular/core';
import { bootstrapWorkerUi,WORKER_UI_LOCATION_PROVIDERS } from '@angular/platform-webworker';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
bootstrapWorkerUi('webworker.chunk.js',WORKER_UI_LOCATION_PROVIDERS);
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MapComponent } from './map/map.component';
import {
WorkerAppModule,
WORKER_APP_LOCATION_PROVIDERS,
WORKER_UI_LOCATION_PROVIDERS
} from '@angular/platform-webworker';
import { APP_BASE_HREF } from '@angular/common'
@NgModule({
declarations: [
AppComponent,
// some components
],
imports: [
BrowserModule,
//some imports
WorkerAppModule,
],
providers: [
{
provide: APP_BASE_HREF,
useValue: '/'
},
WORKER_APP_LOCATION_PROVIDERS
],
bootstrap: [AppComponent]
})
export class AppModule { }
webpack.config.js
const webpack = require('webpack');
const path = require('path');
const { AotPlugin } = require('@ngtools/webpack');
module.exports = {
"entry": {
"main": [
"./src/main.ts",
],
"polyfills": [
"./src/polyfills.ts"
],
"styles": [
"./src/styles.css"
],
"webworker": [
"./src/workerLoader.ts"
]
}
,
"output": {
"path": path.join(process.cwd(), "dist"),
"filename": "[name].bundle.js",
"chunkFilename": "[id].chunk.js"
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all'
}
}
}
},
}
问题是进入浏览器后,我收到一个错误webworker.chunk.js:1 Uncaught ReferenceError: window is not defined,创建此错误的行是(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["webworker"],{
我找到了相同的 question,但对我不起作用。
如何解决问题?
更新:
我的问题被标记为可能重复,但我自己提到了这个问题并强调这对我不起作用。
更新:
AFAIK 网络工作者无法访问诸如 window 之类的全局变量,并且该错误有点奇怪,因为想在网络工作者中访问 window 。我认为 webpack 配置文件中的错误配置是错误的原因。
the error image
【问题讨论】:
-
正如我所说,这对我不起作用,我特意提到了这个问题
-
但问题本身仍然是重复的
-
我做了那里解释的事情,我的问题仍然存在,我该怎么办?
-
@aimar 你能给我在 Angular 6 中实现网络工作者的步骤吗?因为我在过去的 2 天里一直在苦苦挣扎。我经历了这个blog.angularindepth.com/…,但无法使用 ng 弹出
标签: angular webpack angular-cli angular6 web-worker