gpt4 book ai didi

javascript - 提前编译时出现 SystemJS 错误(NgZone 没有提供程序)

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:49 26 4
gpt4 key购买 nike

之前成功使用过Angular的提前编译。现在我已经在我的应用程序中添加了路由和延迟加载,但我无法再让它工作了。

我已经更新我的代码以使用 2.0(最新)版本,并且当我即时编译时它运行良好。但是在提前编译时出现以下错误:

(index):52 Error: (SystemJS) XHR error (404 Not Found) loading https://127.0.0.1:3443/traceur.js Error: XHR error (404 Not Found) loading https://127.0.0.1:3443/traceur.js at XMLHttpRequest.wrapFn [as _onreadystatechange] (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:647:29) at ZoneDelegate.invokeTask (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:236:37) at Zone.runTask (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:136:47) at XMLHttpRequest.ZoneTask.invoke (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:304:33) Error loading https://127.0.0.1:3443/traceur.js Error loading https://127.0.0.1:3443/node_modules/@angular/core/src/linker/ng_module_factory.js as "@angular/core/src/linker/ng_module_factory" from https://127.0.0.1:3443/dist/app/app.module.ngfactory.js

查看app.module.ngfactory.js,有一堆类似于下面的导入:

...
var import0 = require('@angular/core/src/linker/ng_module_factory');
var import1 = require('./app.module');
var import2 = require('@angular/common/src/common_module');
var import3 = require('@angular/core/src/application_module');
var import4 = require('@angular/platform-browser/src/browser');
var import5 = require('@angular/forms/src/directives');
var import6 = require('@angular/forms/src/form_providers');
...

我注意到如果我改变

var import0 = require('@angular/core/src/linker/ng_module_factory');

import * as import0 from '@angular/core/src/linker/ng_module_factory';

SystemJS 接受这一行并为下一行触发相同的错误,即

var import1 = require('./app.module');

所以我一直试图让 SystemJS 识别这种 CommonJS 样式格式,但这只会导致其他错误,没有一个比上面的错误更清楚。

编辑:当我切换到在 tsconfig 中使用 es2015 模块并运行应用程序时,我收到(可怕的)traceur.js 错误:

https://127.0.0.1:3443/traceur.js Failed to load resource: the server responded with a status of 404 (Not Found) 2016-09-26 10:11:19.842 127.0.0.1/:52 Error: (SystemJS) XHR error (404 Not Found) loading https://127.0.0.1:3443/traceur.js Error: XHR error (404 Not Found) loading https://127.0.0.1:3443/traceur.js at XMLHttpRequest.wrapFn [as _onreadystatechange] (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:647:29) at ZoneDelegate.invokeTask (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:236:37) at Zone.runTask (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:136:47) at XMLHttpRequest.ZoneTask.invoke (https://127.0.0.1:3443/node_modules/zone.js/dist/zone.js:304:33) Error loading https://127.0.0.1:3443/traceur.js Error loading https://127.0.0.1:3443/dist/app/main.js

我以前遇到过这个问题,并通过删除包含 import 语句的注释掉的代码解决了这个问题。这次我的代码中没有这样的行。

这是我的 systemjs.config.js:

(function(global) {
System.defaultJSExtensions = true;
System.defaultExtension = 'js';

// map tells the System loader where to look for things
// N.B. the dots at the start of the paths are very important!
var map = {
'app': './dist/app',
'@angular': './node_modules/@angular',
'rxjs': './node_modules/rxjs'
};

// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' }
};
var ngPackageNames = [
'common',
'compiler',
'compiler-cli',
'core',
'forms',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'tsc-wrapped',
'upgrade'
];
function packUmd(pkgName) {
packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
}
var setPackageConfig = packUmd;
// Add package entries for angular packages
ngPackageNames.forEach(setPackageConfig);
console.log("SystemJS packages:", packages);
var config = {
map: map,
packages: packages
};

System.config(config);

})(this);

编辑 2:当我在 systemjs.config.js 中将 'app' 的格式设置为 'cjs' 时,即

'app': { main: 'main.js',  defaultExtension: 'js', format: 'cjs' },

使用commonjs编译代码时,不再出现上面提到的app.module.ngfactory.js中的错误。

但是,当在浏览器中运行时,出现以下错误(在安装 traceur 之后;没有 traceur 我只是得到上面的 traceur 错误):

(index):52 Error: (SystemJS) No provider for NgZone! Error: No provider for NgZone! at NoProviderError.Error (native) at NoProviderError.BaseError [as constructor] (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:1255:38) at NoProviderError.AbstractProviderError [as constructor] (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:1739:20) at new NoProviderError (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:1770:20) at ReflectiveInjector_._throwOrNull (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:3366:23) at ReflectiveInjector_._getByKeyDefault (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:3394:29) at ReflectiveInjector_._getByKey (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:3357:29) at ReflectiveInjector_.get (https://147.252.67.223:3443/node_modules/@angular/core/bundles/core.umd.js:3166:25) at AppModuleInjector.createInternal (https://147.252.67.223:3443/dist/app/app.module.ngfactory.js:610:69) at AppModuleInjector.NgModuleInjector.create (https://147.252.67.223:3443/node_modules/@angular/core/src/linker/ng_module_factory.js!transpiled:96:32) Evaluating https://147.252.67.223:3443/dist/app/main.js Error loading https://147.252.67.223:3443/dist/app/main.js

我的 tsconfig.json:

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"removeComments": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"rootDir":"./src",
"outDir":"./dist"
},
"angularCompilerOptions": {
"genDir": ".",
"debug": true,
"skipMetadataEmit": true
}
}

非常感谢任何帮助。

最佳答案

由于 systemjs.config.js,我从 Angular RC6 切换到 2.0.0 时遇到了类似的问题。

我最终根据最新的快速入门指南重做了整个 systemjs.config.js:https://angular.io/docs/ts/latest/quickstart.html#!#add-config-files有一些修改。

我在您的 systemjs.config.js 中看到的一个问题是缺少对以下包的引用:

  • es6-shim(或 core-js)
  • 反射(reflect)元数据
  • zone.js

以下是我的systemjs.config.js

(function (global) {
System.config({
paths: {
'npm:': '/node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: '/app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'es6-shim': 'npm:es6-shim',
'reflect-metadata': 'npm:reflect-metadata',
'rxjs': 'npm:rxjs',
'zone.js': 'npm:zone.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
'app': { main: 'main.js', defaultExtension: 'js' },
'es6-shim': { main: 'es6-shim.min.js', defaultExtension: 'js' },
'reflect-metadata': { main: 'Reflect.js', defaultExtension: 'js' },
'rxjs': { main: 'bundles/Rx.min.js', defaultExtension: 'js' },
'zone.js': { main: 'dist/zone.min.js', defaultExtension: 'js' }
}
});
})(this);

关于javascript - 提前编译时出现 SystemJS 错误(NgZone 没有提供程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39621343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com