Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 routes TypeError: Cannot read property 'forRoot'

angular2 rc.5

I am getting TypeError: Cannot read property 'forRoot' of undefined(…) when i investigated i found that RouterModule is undefined during execution.

Following is my app.route.ts

import {Routes, RouterModule } from '@angular/router';

//const routes: Routes = [];
import {HelloComponent} from './hello.component';
import {IncComponent} from './inc.component';
import {AdditionComponent} from './addition.component';
import {RomanComponent} from './roman.component';

let routes: Routes = [

    {
        path: 'hello',
        component: HelloComponent
    },
    {
        path: '',
        redirectTo: '/hello',
        pathMatch: 'full'
    },
    {
        path: 'increment',
        component: IncComponent
    },{
        path: 'adder',
        component: AdditionComponent
    },{
        path: 'roman',
        component: RomanComponent
    }
];


export const routing = RouterModule.forRoot(routes); // error on this line

Following is my app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import {routing} from './app.routes';
import {HelloComponent} from './hello.component';
import {IncComponent} from './inc.component';
import {AdditionComponent} from './addition.component';
import {RomanComponent} from './roman.component';

@NgModule({
    imports:[
        BrowserModule,
        routing
    ],
    declarations: [
        AppComponent,
        HelloComponent,
        IncComponent,
        AdditionComponent,
        RomanComponent
    ],
    bootstrap:    [ AppComponent ]
})
export class AppModule { }

and main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    //import { HTTP_PROVIDERS } from '@angular/http';
    import { AppModule } from './app.module';

    platformBrowserDynamic().bootstrapModule(AppModule);
like image 596
d-man Avatar asked Oct 19 '22 04:10

d-man


2 Answers

I had the same problem. Maybe it's corny, but in my case, problem was solved after I cleared the browser cache.

like image 169
bobah75 Avatar answered Nov 11 '22 11:11

bobah75


I had this same issue working in a .NET Core web application in Visual Studio 2015. I upgraded my packages from rc4 to rc5, did an npm install and saw this error after I refreshed my browser. I eventually realized that I forgot to copy the updated packages from node_modules to my lib folder inside wwwroot (I have a gulp task that does this). After running the task to update the libs everything was back to normal. Hope this helps someone.

like image 42
Jerms Avatar answered Nov 11 '22 13:11

Jerms