Angular 2 Tour of Heroes, Step 7 error - angularjs

I looked through the forums, but couldn't find anything relevant.
I just started the tutorial 3 days ago, got to Step 7, and when I add the InMemoryWebApiModule to the mix (as detailed in the steps), I get this error:
All of my config files are directly from what was provided in the tutorial, but I'll provide them here anyway.
Any ideas as to what's going on?
Here is my package.json:
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"#angular/common": "~2.1.1",
"#angular/compiler": "~2.1.1",
"#angular/core": "~2.1.1",
"#angular/forms": "~2.1.1",
"#angular/http": "~2.1.1",
"#angular/platform-browser": "~2.1.1",
"#angular/platform-browser-dynamic": "~2.1.1",
"#angular/router": "~3.1.1",
"#angular/upgrade": "~2.1.1",
"angular-in-memory-web-api": "~0.1.13",
"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": {
"#types/core-js": "^0.9.34",
"#types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
Here is app.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';
import { AppRoutingModule } from './app-routing.module'
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';
import { DashboardComponent } from './dashboard.component';
#NgModule({
imports: [
AppRoutingModule,
BrowserModule,
FormsModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService)
],
declarations: [
AppComponent,
HeroDetailComponent,
HeroesComponent,
DashboardComponent
],
providers: [ HeroService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Here is systemjs.config.js:
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(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/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
'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);

Take a look at the CHANGELOG. You need to use the umd bundle, and remove it from the packages
In systemjs.config.js you should change the mapping to:
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
then delete from packages:
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}

Related

How to fix 'mat-icon' is not a known element error?

I'm pretty new to Angular, doing my first app. I wanted to use Angular Material library icons and encountered an error saying that 'mat-icon' is not a known element:.
Full error here
error NG8001: 'mat-icon' is not a known element:
1. If 'mat-icon' is an Angular component, then verify that it is part of this module.
2. If 'mat-icon' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message.
11 <mat-icon>favorite</mat-icon>
From what I understood, the problem comes when you don't import MatIconModule in App module.
However, I still get the same error even after importing it.
Here is what I have tried so far.
html file, obviously error refers to this component, where I use the icon
<div>
<div>
<button mat-fab disabled aria-label="Example icon button with a heart icon">
<mat-icon>favorite</mat-icon>
</button>
</div>
</div>
app.modules.ts
import { BrowserModule } from "#angular/platform-browser";
import { NgModule } from "#angular/core";
import { HttpClientModule } from "#angular/common/http";
import { MatIconModule } from "#angular/material/icon";
import { MatButtonModule } from "#angular/material/button";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { BrowserAnimationsModule } from "#angular/platform-browser/animations";
import { MatToolbarModule } from "#angular/material/toolbar";
import { MatCardModule } from "#angular/material/card";
#NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatCardModule,
MatToolbarModule,
HttpClientModule,
MatIconModule,
MatButtonModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
And Package.json
{
"name": "angular-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"#angular/animations": "~10.2.5",
"#angular/cdk": "~10.2.7",
"#angular/common": "~10.2.5",
"#angular/compiler": "~10.2.5",
"#angular/core": "~10.2.5",
"#angular/forms": "~10.2.5",
"#angular/material": "^10.0.0",
"#angular/material-moment-adapter": "^10.0.0",
"#angular/platform-browser": "~10.2.5",
"#angular/platform-browser-dynamic": "~10.2.5",
"#angular/router": "~10.2.5",
"hammerjs": "^2.0.8",
"rxjs": "~6.6.7",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"#angular-devkit/build-angular": "~0.1002.4",
"#angular/cli": "~10.2.4",
"#angular/compiler-cli": "~10.2.5",
"#angular/language-service": "~10.2.5",
"#types/node": "^12.11.1",
"#types/jasmine": "~3.3.8",
"#types/jasminewd2": "~2.0.3",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~7.0.0",
"tslint": "~6.1.0",
"typescript": "~4.0.8"
}
}
I also tried npm i, but didn't help either.
Let me know please, if I should include more files so that you can understand the problem better.
Any help will be appreciated.
Update
Now instead of importing Material components in app.module.ts I've created `material.module.ts
and now import looks like so.
import { BrowserModule } from "#angular/platform-browser";
import { NgModule } from "#angular/core";
import { HttpClientModule } from "#angular/common/http";
import { MaterialModule } from "./material/material.module";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { BrowserAnimationsModule } from "#angular/platform-browser/animations";
#NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MaterialModule
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
material.module.ts
import { NgModule } from "#angular/core";
import { MatButtonModule } from "#angular/material/button";
import { MatIconModule } from "#angular/material/icon";
const MaterialComponents = [
MatButtonModule,
MatIconModule,
];
#NgModule({
imports: [MaterialComponents],
exports: [MaterialComponents],
providers: [
{
provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS,
useValue: {
strict: true,
useUtc: true,
},
},
],
declarations: [],
})
export class MaterialModule {}
Unfortunately, even though it looks better but doesn't work either.
I feel like there is version mismatch.
Have you already add the font in the index.html
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#300;400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
Here is an example
Simply import MatIconModule and add MatIconModule in your imports array in app.module.ts

Error loading /ng2-bootstrap/alert.js as "ng2-bootstrap/alert" from /Scripts/app.modules.js

I have not been able to find a solution by researching the error Error loading /ng2-bootstrap/alert.js as "ng2-bootstrap/alert" from /Scripts/app.modules.js.
I have tried these suggested solutions in System.config.js but they do not solve the problem:
map: {
1. 'ng2-bootstrap': 'node_modules/ng2-bootstrap' or
2. 'ng2-bootstrap': 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.umd.js'
}
packages: {
1. 'ng2-bootstrap': { defaultExtension: 'js' },
2. 'ng2-bootstrap': { format: 'cjs', main: 'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },
}
This is my **System.config.js** which I have read is likely the source of the issue.
(function (global) {
window.System.config({
paths: {
// paths serve as alias
'npm:': '/libs/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: '/Scripts',
// 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
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'moment': 'node_modules/moment/moment.js',
'ng2-bootstrap': 'node_modules/ng2-bootstrap'
},
// 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'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'ng2-bootstrap': { format: 'cjs', main:
'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },
'moment': { main: 'moment.js', defaultExtension: 'js' }
}
});
var ngBootstrapPackageNames = [
'accordion',
'alert',
'bundles',
'buttons',
'carousel',
'collapse',
'dropdown',
'esm',
'modal',
'pagination',
'popover',
'progressbar',
'rating',
'tabset',
'timepicker',
'tooltip',
'typeahead',
'util'
];
function ngBootstrapPackIndex(pkgName) {
//packages['#ng-bootstrap/ng-bootstrap/' + pkgName] = { main:
'index.js', defaultExtension: 'js' };
}
ngBootstrapPackageNames.forEach(ngBootstrapPackIndex);
})(this);
This is my **app.module.ts** which has been implied but does not seem to help.
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { AppRoutingModule } from './app.routing';
import { AppComponent } from './app.component';
import { CollapsiblePanelComponent } from
'./bootstrap- components/collapsiblePanel.component';
import { InputGroupAddOnComponent } from
'./bootstrap-components/input-group-addon.component';
import { CreateRegimenComponent } from
'./create-regimen/create-regimen.component'
import { AlertComponent } from 'ng2-bootstrap/alert';
import { CollapseModule } from 'ng2-bootstrap/collapse';
import Approuting = require("./app.routing");
//import { NgbModule } from '#ng-bootstrap/ng-bootstrap';
#NgModule({
imports: [
CollapseModule,
AlertComponent,
FormsModule,
HttpModule,
// InMemoryWebApiModule.forRoot(InMemoryDataService),
AppRoutingModule,
BrowserModule],
declarations: [
AppComponent,
CollapsiblePanelComponent,
InputGroupAddOnComponent,
CreateRegimenComponent,
CollapseModule
],
bootstrap: [
AppComponent,
CollapsiblePanelComponent,
AlertComponent,
CollapseModule
]
})
export class AppModule { }
This is my Packages.json where I have read that "#ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20", is the correct syntax. This does not help either.
{
"version": "1.0.0",
"name": "aspnet",
"private": true,
"scripts": {
"postinstall": "typings install",
"typings": "typings"
},
"dependencies": {
"#angular/common": "~2.1.0",
"#angular/compiler": "~2.1.0",
"#angular/core": "~2.1.0",
"#angular/forms": "~2.1.0",
"#angular/http": "~2.1.0",
"#angular/platform-browser": "~2.1.0",
"#angular/platform-browser-dynamic": "~2.1.0",
"#angular/router": "~3.1.0",
"#angular/upgrade": "~2.1.0",
"#ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20",
"#types/core-js": "^0.9.35",
"#types/reflect-metadata": "0.0.5",
"angular-in-memory-web-api": "~0.1.5",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"jquery": "^3.1.1",
"moment": "^2.17.1",
"ng2-bootstrap": "^1.3.3",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"#types/jquery": "^2.0.40",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.0",
"gulp-tsc": "^1.2.0",
"gulp-typescript": "^2.13.6",
"path": "^0.12.7",
"typescript": "^2.0.3",
"typings": "^1.4.0",
"webpack": "^2.2.1"
}
}
Here is my node_modules directory
How do I get ng2_bootstrap to load without error 404
I finally figured out how to load ng2-bootstrap. For those out there trying to do this there are many many suggestions out there that simply don't work, at least as of February 27, 2017. So it is very frustrating for anybody trying.
This is what I somehow pieced together from the many suggestions I found. The most important suggestion being from ng2-bootstrap themselves. (Although ng2-bootstrap is not complete either).
System.js
(function (global) {
window.System.config({
paths: {
// paths serve as alias
'npm:': '/libs/',
'ng2-bootstrap/ng2-bootstrap': "node_modules/ng2-bootstrap/ng2-bootstrap"
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: '/Scripts',
// 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
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
// ng2-bootstrap
**'moment': 'node_modules/moment',
'ng2-bootstrap': 'node_modules/ng2-bootstrap'**
},
// 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'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'ng2-bootstrap': { format: 'cjs', main: 'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },
'moment': { main: 'moment.js', defaultExtension: 'js' }
}
});
})(this);
app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { Routes, RouterModule } from '#angular/router';
import { appComponents as AppComponents, appRoutes as AppRoutes } from './app.routing';
import { AppComponent } from './app.component';
import { **ButtonsModule, DatepickerModule, AlertModule** } from **'ng2-bootstrap';**
import Approuting = require("./app.routing");
#NgModule({
imports: [
FormsModule,
HttpModule,
// InMemoryWebApiModule.forRoot(InMemoryDataService),
RouterModule.forRoot(AppRoutes),
AlertModule.forRoot(),
DatepickerModule.forRoot(),
ButtonsModule.forRoot(),
BrowserModule],
declarations: [
AppComponent,
IndexComponent,
IndexPatientsComponent
],
providers: [],
bootstrap: [
AppComponent
]
})
export class AppModule { }

ctorParameters.map is not a function

My package.json
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"#angular/common": "^2.1.1",
"#angular/compiler": "^2.1.1",
"#angular/core": "^2.1.1",
"#angular/forms": "^2.1.1",
"#angular/http": "^2.1.1",
"#angular/platform-browser": "^2.1.1",
"#angular/platform-browser-dynamic": "^2.1.1",
"#angular/router": "^3.1.1",
"#angular/upgrade": "^2.1.1",
"angular-in-memory-web-api": "^0.1.13",
"bootstrap": "^3.3.7",
"chart.js": "^2.4.0",
"core-js": "^2.4.1",
"ng2-charts": "^1.4.4",
"ng2-webstorage": "^1.5.0",
"reflect-metadata": "^0.1.8",
"rxjs": "^5.0.0-rc.4",
"systemjs": "0.19.39",
"zone.js": "^0.7.2"
},
"devDependencies": {
"#types/core-js": "^0.9.34",
"#types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
My app.module.ts
import { NgModule } from '#angular/core';
import {BrowserModule} from '#angular/platform-browser';
import {FormsModule, ReactiveFormsModule} from '#angular/forms';
import {HttpModule} from '#angular/http';
import {routing} from './app.routing';
import {ChartsModule} from 'ng2-charts/ng2-charts';
import {HttpHelper} from './shared/http-helper'
import {SpinnerComponent} from './misc/spinner/spinner.component';
import {PaginationComponent} from './misc/pagination/pagination.component';
import { AppComponent } from './app.component';
import {NavbarComponent} from './navbar/navbar.component';
import {AuthenticationComponent} from './components/authentication/authentication.component';
> import {HomeComponent} from
> './home/home.component'; import {HotelsComponent}
> from './components/hotel/hotels.component'; import {NewHotelComponent}
> from './components/hotel/new-hotel.component'
>
> import {AuthenticationService} from
> './components/authentication/authentication.service'; import
> {HotelsService} from
> './components/hotel/hotels.service';
>
> #NgModule({ imports: [
> BrowserModule
> , FormsModule
> , ReactiveFormsModule
> , HttpModule
> , routing
> , ChartsModule
> , Ng2Webstorage
> ], declarations: [
> AppComponent
> , NavbarComponent
> , AuthenticationComponent
> , HomeComponent
> , HotelsComponent
> , NewHotelComponent
> , SpinnerComponent
> , PaginationComponent
> ], providers :[
> HttpHelper
> , AuthenticationService
> , HotelsService], bootstrap: [ AppComponent ] }) export class AppModule { }
My systemjs.config.js
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(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/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
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
'ng2-charts' : 'npm:ng2-charts',
'underscore' : 'npm:underscore',
},
// 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'
},
'ng2-charts':{
defaultExtension: 'js'
},
'underscore':{
defaultExtension: 'js'
}
}
});
})(this);
My application works fine with above code. Now I want add session storage to app so i used Ng2Webstorage (v1.5). I installed it using NPM. As soon as, I added following code:
Addition to app.module.ts
import {Ng2Webstorage} from 'ng2-webstorage';
...
...
#NgModule({
imports: [
...
,....
, Ng2Webstorage
],
...
})
Addition to systemjs.config.js
map:{
......
......
'ng2-webstorage' : 'npm:ng2-webstorage'
}
packages:{
....
....
,
'ng2-webstorage': {
main: 'bundles/core.umd.js',
defaultExtension: 'js'
}
}
I started receiving following error. I tried removing Ng2Webstorage and my app works fine.
"(SystemJS) ctorParameters.map is not a function TypeError:
ctorParameters.map is not a function
at ReflectionCapabilities.parameters (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:1780:49)
at Reflector.parameters (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:1922:48)
at CompileMetadataResolver.getDependenciesMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14534:56)
at CompileMetadataResolver.getTypeMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14499:28)
at eval (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14642:43)
at Array.forEach (native)
at CompileMetadataResolver.getProvidersMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14622:21)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14381:60)
at eval (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14324:52)
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14311:46)
at RuntimeCompiler._compileComponents (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:17063:49)
at RuntimeCompiler._compileModuleAndComponents (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:17001:39)
at RuntimeCompiler.compileModuleAsync (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:16992:23)
at PlatformRef_._bootstrapModuleWithZone (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:6684:29)
Evaluating http://localhost:3000/app/main.js Error loading
http://localhost:3000/app/main.js"
You can directly use setItem, getItem and clear methods of sessionStorage as below :
To set abdheajskke token value with key token in session storage use :
sessionStorage.setItem("token", "abdheajskke")
To get token value from session storage use :
sessionStorage.getItem("token")
Or you can create your own service to use sessionStorage. Below is the example of simple minimal service which you can use :
import { Injectable } from '#angular/core';
#Injectable()
export class StorageService {
public setItem(key: string, data: any) {
sessionStorage.setItem(key, JSON.stringify(data));
}
public getItem(key: string) {
if (!!sessionStorage.getItem(key)) {
return sessionStorage.getItem(JSON.parse(key));
} else {
return false;
}
}
public clear() {
sessionStorage.clear();
return true;
}
public removeItem(key: string) {
sessionStorage.removeItem(key);
return true;
}
}

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. 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:
app.module.ts:1Uncaught ReferenceError: require is not defined
When I switch the module to system, I get the following error:
Uncaught TypeError: Invalid System.register call. Anonymous System.register calls can only be made by modules loaded by SystemJS.import and not via script tags.
And when I switch module to umd, everything works fine. But given that angular themselves suggest using commonjs, I'm concerned that umd is not the right answer. However, if I'm wrong about that and umd is perfectly fine, I'd love to hear a good explanation as to why.
Here's my code to reproduce my issue:
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
,
"exclude": [
"node_modules"
]
}
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 :
(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 :
{
"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 :
angular.module('app', []);
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 :
angular.module('app')
.controller('appCtrl', ['$scope', function($scope) {
$scope.hello = "howdy worldy";
}]);
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. In this way you need pass a forwardRef instance, something like this:
const upgradeAdapter = new UpgradeAdapter(forwardRef(() => AppModule));
finally you need import the forwardRef
import {NgModule, forwardRef} from '#angular/core';
First, thanks Andres. I need to read up a bit about the forwardRef to understand what that does. 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'). 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. I had changed those typescript files to use export so I could import them into my angular 2 components. This led me to also change my /// in the angular 1 files to use import. Unfortunately, this gave me an "undefinedModule" error. The solution (in my case) is not to use export on typescript files unless they are ONLY used with the angular 2 components. Meaning, in some angular 2 components, I'm actually using the /// and not the import.
Just thought other people might find that useful.

Angular2 Tutorial (Tour of Heroes): Cannot find module './app-routing.module'

I am going through the NG2 tutorial on here, but I've hit a stumbling block.
When I try and import my AppRoutingModule in my app.module.ts file I get a 'Cannot find module './app-routing.module' error. I have seen this post on here, amongst other solutions, but they all seem to relate to systemjs and not webpack.
Here is my package.json:
{
"name": "heroes",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"#angular/common": "2.0.0",
"#angular/compiler": "2.0.0",
"#angular/core": "2.0.0",
"#angular/forms": "2.0.0",
"#angular/http": "2.0.0",
"#angular/platform-browser": "2.0.0",
"#angular/platform-browser-dynamic": "2.0.0",
"#angular/router": "3.0.0",
"angular2-in-memory-web-api": "0.0.21",
"core-js": "^2.4.1",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.23"
},
"devDependencies": {
"#types/jasmine": "^2.2.30",
"angular-cli": "1.0.0-beta.15",
"codelyzer": "~0.0.26",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.5",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "2.0.2"
}
}
Here is my app.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { RouterModule } from '#angular/router';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
import { AppRoutingModule } from './app-routing.module'; //ERROR
...
#NgModule({
declarations: [
AppComponent,
HeroDetailComponent,
HeroesComponent,
DashboardComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
InMemoryWebApiModule.forRoot(InMemoryDataService), //ERROR: Cannot find name 'InMemoryDataServiceInMemoryDataService
RouterModule.forRoot([
{
path: '', redirectTo: '/dashboard', pathMatch: 'full'
},
{
path: 'dashboard', component: DashboardComponent
},
{
path: 'detail/:id', component: HeroDetailComponent
},
{
path: 'heroes', component: HeroesComponent
}
])
],
providers: [HeroService],
bootstrap: [AppComponent]
})
export class AppModule { }
Here is my specs (I am using angular CLI):
angular-cli: 1.0.0-beta.15
node: 4.2.6
os: linux x64 (ubuntu)
Not sure what else I can try? I guess I need to import the module somehow?
I did try and run:
npm install angular-route
But this led to:
├──UNMET PEER DEPENDENCY
#angular/common#2.0.0
├──UNMET PEER DEPENDENCY
#angular/core#2.0.0
├──UNMET PEER DEPENDENCY
#angular/platform-browser#2.0.0
└── angular-route#1.5.8
Anyone have any suggestions?
Checkout the live example with all the files
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'detail/:id', component: HeroDetailComponent },
{ path: 'heroes', component: HeroesComponent }
];
#NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
According to comments, it seems that you might be missing the app-routing file altogether. Perhaps you missed the piece describing that, or sometimes the documentation might be outdated. Please check the plunker showing a working, live example with the needed files.
Right before making a new angular project, the CLI prompts you to either include routing or not, like this:
Would you like to add Angular routing? Yes
Make sure you type in Y when it does so, or else your project won't be made with routing files.

Resources