[英]Angular 1 and 2 hybrid app - “require is not defined”
I'm trying to upgrade a giant client-side app from Angular 1.x to Angular 2, and I've hit a really annoying roadblock. 我正在尝试将一个巨大的客户端应用程序从Angular 1.x升级到Angular 2,我遇到了一个非常烦人的障碍。 I've recreated the issue with a dummy, light-weight project (files pasted below), but let me just explain the problem first. 我用一个虚拟的,轻量级的项目(下面粘贴的文件)重新创建了这个问题,但是让我先解释一下这个问题。
Basically, when my tsconfig.json
specifies module as commonjs
, I get the following error in my chrome dev console: 基本上,当我的tsconfig.json
模块指定为commonjs
,我在chrome dev控制台中收到以下错误:
app.module.ts:1Uncaught ReferenceError: require is not defined app.module.ts:1 Uncaught ReferenceError:未定义require
When I switch the module to system
, I get the following error: 当我将模块切换到system
,我收到以下错误:
Uncaught TypeError: Invalid System.register call. 未捕获的TypeError:无效的System.register调用。 Anonymous System.register calls can only be made by modules loaded by SystemJS.import and not via script tags. 匿名System.register调用只能由SystemJS.import加载的模块进行,而不能通过脚本标记进行。
And when I switch module to umd
, everything works fine. 当我将模块切换到umd
,一切正常。 But given that angular themselves suggest using commonjs
, I'm concerned that umd
is not the right answer. 但鉴于角度本身建议使用commonjs
,我担心umd
不是正确的答案。 However, if I'm wrong about that and umd
is perfectly fine, I'd love to hear a good explanation as to why. 但是,如果我错了和umd
是完全没有问题,我很乐意听到一个很好的解释为什么。
Here's my code to reproduce my issue: 这是重现我的问题的代码:
tsconfig.json: tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
,
"exclude": [
"node_modules"
]
}
typings.json: typings.json:
{
"globalDependencies": {
"angular": "registry:dt/angular#1.5.0+20160922195358",
"core-js": "registry:dt/core-js#0.0.0+20160725163759",
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"jquery": "registry:dt/jquery#1.10.0+20160929162922",
"node": "registry:dt/node#6.0.0+20160909174046"
}
}
systemjs.config.js : systemjs.config.js:
(function (global) {
System.config({
paths: {
// paths serve as alias
'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/ ': '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
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
package.json : package.json:
{
"name": "mattsApp",
"version": "0.0.1",
"dependencies": {
"angular": "^1.5.8",
"@angular/common": "~2.0.2",
"@angular/compiler": "~2.0.2",
"@angular/core": "~2.0.2",
"@angular/forms": "~2.0.2",
"@angular/http": "~2.0.2",
"@angular/platform-browser": "~2.0.2",
"@angular/platform-browser-dynamic": "~2.0.2",
"@angular/router": "~3.0.2",
"@angular/upgrade": "~2.0.2",
"angular-in-memory-web-api": "~0.1.5",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3",
"typings":"^1.4.0"
},
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
}
}
app.js : app.js:
angular.module('app', []);
app.module.ts : app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { upgradeAdapter } from './upgrade_adapter';
@NgModule({
imports: [ BrowserModule ]
})
export class AppModule { }
upgradeAdapter.bootstrap(document.body, ['app'], {strictDi: true});
appCtrl.ts : appCtrl.ts:
angular.module('app')
.controller('appCtrl', ['$scope', function($scope) {
$scope.hello = "howdy worldy";
}]);
upgrade_adapter.ts : upgrade_adapter.ts:
import { UpgradeAdapter } from '@angular/upgrade';
import {AppModule} from "./app.module";
export const upgradeAdapter = new UpgradeAdapter(AppModule);
What am I missing? 我错过了什么?
you need instantiate the UpgradeAdapter with a non null parametter. 您需要使用非null参数实例化UpgradeAdapter。 In this way you need pass a forwardRef instance, something like this: 这样你需要传递一个forwardRef实例,如下所示:
const upgradeAdapter = new UpgradeAdapter(forwardRef(() => AppModule));
finally you need import the forwardRef 最后你需要导入forwardRef
import {NgModule, forwardRef} from '@angular/core';
First, thanks Andres. 首先,谢谢Andres。 I need to read up a bit about the forwardRef to understand what that does. 我需要阅读一下有关forwardRef的内容,以了解它的作用。 But it turns out that the answer in my particular case was something different. 但事实证明,在我的特定情况下,答案是不同的。
I didn't post my index.html here (simple oversight), but the problem was that I wasn't loading my modules with the System.import('app'). 我没有在这里发布我的index.html(简单的疏忽),但问题是我没有使用System.import('app')加载我的模块。 Embarrassing error, I have to admit. 令人尴尬的错误,我不得不承认。 So the answer is to add that line. 所以答案是添加该行。
I should note that this led to a different error which I solved, but I'll point it out here in case others have a similar issue. 我应该注意到这导致我解决了一个不同的错误,但我会在这里指出它以防其他人有类似的问题。 Since this is a hybrid angular 1 and 2 app, I have typescript files that are sometimes used by angular 1 controllers / directives / etc, and also by angular 2 components. 由于这是一个混合角度1和2应用程序,我有一些打字稿文件,有时由角度1控制器/指令/等使用,也有角度2组件。 I had changed those typescript files to use export so I could import them into my angular 2 components. 我已将这些打字稿文件更改为使用导出,因此我可以将它们导入到我的angular 2组件中。 This led me to also change my /// in the angular 1 files to use import. 这导致我也在角度1文件中更改我的///以使用导入。 Unfortunately, this gave me an "undefinedModule" error. 不幸的是,这给了我一个“undefinedModule”错误。 The solution (in my case) is not to use export on typescript files unless they are ONLY used with the angular 2 components. 解决方案(在我的例子中)不是对typescript文件使用export,除非它们仅与angular 2组件一起使用。 Meaning, in some angular 2 components, I'm actually using the /// and not the import. 意思是,在一些有角度的2个组件中,我实际上是在使用///而不是导入。
Just thought other people might find that useful. 只是认为其他人可能会觉得有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.