I am demoing an Angular 2 app for my team, showing the minimal setup required for an app to function.
This includes utilizing the http://unpkg.com CDN for all libraries and the following two files:
index.html main.ts
Below are the index.html and main.ts files:
index.html
<!DOCTYPE html>
<head>
<!-- polyfills must load before zone.js -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.6.12"></script>
<script src="https://unpkg.com/typescript@2.0.0"></script>
<script src="https://unpkg.com/systemjs@0.19.37/dist/system.src.js"></script>
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
map: {
'rxjs': 'https://unpkg.com/rxjs@5.0.0-beta=12',
'@angular/core': 'https://unpkg.com/@angular/core@2.0.0',
'@angular/common': 'https://unpkg.com/@angular/common@2.0.0',
'@angular/compiler': 'https://unpkg.com/@angular/compiler@2.0.0',
'@angular/platform-browser': 'https://unpkg.com/@angular/platform-browser@2.0.0',
'@angular/platform-browser-dynamic': 'https://unpkg.com/@angular/platform-browser-dynamic@2.0.0'
},
packages: {
'@angular/core': { main: 'index.js' },
'@angular/common': { main: 'index.js' },
'@angular/compiler': { main: 'index.js' },
'@angular/platform-browser': { main: 'index.js' },
'@angular/platform-browser-dynamic': { main: 'index.js' }
}
});
System.import('main.ts');
</script>
</head>
<body>
<hello-world></hello-world>
</body>
</html>
main.ts
import { Component } from '@angular/core';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
@Component({
selector: 'hello-world',
template: '<h1>Hello {{ name }}!</h1>'
})
class HelloWorldComponent {
name: string;
constructor() {
this.name = 'Angular Demo';
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ HelloWorldComponent ],
bootstrap: [ HelloWorldComponent ]
})
export class AppModule {}
platformBrowserDynamic().bootstrapModule(AppModule);
After loading both files on a server, and loading localhost:8080
The index.html file loads, registers the systemjs, tranpsiles the main.ts on the fly and should return Hello, Angular Demo!, however I receive the following error in the developer console (run on Google Chrome browser):
Unhandled Promise rejection: (SystemJS) HelloWorldComponent is not defined
ReferenceError: HelloWorldComponent is not defined
at eval (http://localhost:8080/main.ts!transpiled:47:40)
at execute (http://localhost:8080/main.ts!transpiled:53:14)
at ZoneDelegate.invoke (https://unpkg.com/zone.js@0.6.12:323:29)
Error loading http://localhost:8080/main.ts ; Zone: <root> ; Task: Promise.then ; Value: Error: (SystemJS) HelloWorldComponent is not defined
ReferenceError: HelloWorldComponent is not defined
at eval (http://localhost:8080/main.ts!transpiled:47:40)
at execute (http://localhost:8080/main.ts!transpiled:53:14)
at ZoneDelegate.invoke (https://unpkg.com/zone.js@0.6.12:323:29)
Error loading http://localhost:8080/main.ts
consoleError @ zone.js@0.6.12:461
_loop_1 @ zone.js@0.6.12:490
drainMicroTaskQueue @ zone.js@0.6.12:494
ZoneTask.invoke @ zone.js@0.6.12:426
I've gone over possible issues for the past 24 hours and cannot find the solution to this issue.
Any idea why I receive this particular error would be helpful.
UPDATE
I believe this Component error was some sort of cache issue during my coding, as after I restarted my server (http-server -> localhost:8080), I no longer receive this error, and now receive the following error:
Unhandled Promise rejection: Zone.assertZonePatched is not a function ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Zone.assertZonePatched is not a function
at new NgZoneImpl (ng_zone_impl.js:20)
at new NgZone (ng_zone.js:100)
at PlatformRef_._bootstrapModuleFactoryWithZone (application_ref.js:262)
at eval (application_ref.js:304)
at ZoneDelegate.invoke (zone.js@0.6.12:323)
at Zone.run (zone.js@0.6.12:216)
at zone.js@0.6.12:571
at ZoneDelegate.invokeTask (zone.js@0.6.12:356)
at Zone.runTask (zone.js@0.6.12:256)
at drainMicroTaskQueue (zone.js@0.6.12:474)
at XMLHttpRequest.ZoneTask.invoke (zone.js@0.6.12:426)
consoleError @ zone.js@0.6.12:461
_loop_1 @ zone.js@0.6.12:490
drainMicroTaskQueue @ zone.js@0.6.12:494
ZoneTask.invoke @ zone.js@0.6.12:426
Not sure if I should answer my question or just delete it (open to suggestions).
However, because everyone reaches a particular issue different ways (mine was resolving from one error to another), I will leave it here for posterity.
Again, open to suggestions.
SOLUTION
After receiving a new Zone.assertZonePatched is not a function error, I have found the answer at the following StackOverflow thread:
Zone.assertPatched is not a function
The issue was in my reference to the zone.js library:
<script src="https://unpkg.com/zone.js@0.6.12"></script>
According to the thread, if I am using Angular 2.0.0 or above, I will need to update my zone.js library version to the following:
<script src="https://unpkg.com/zone.js@0.6.23"></script>
That solved the issue.