Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'router-outlet' is not a known element Angular2

Repo: https://github.com/leongaban/lifeleveler.io

Not sure why I'm getting this error, I have Router imported in my app.component.ts

I'm trying to use the app.component to hold the main <router-outlet>, and serve up the login view first.

enter image description here

app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { routing } from './app.routing';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { AuthService } from './shared/services/auth.service';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/observable/throw';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        RouterModule,
        routing
    ],
    declarations: [
        AppComponent,
        LoginComponent
    ],
    providers: [
        AuthService,
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule {}

app.component.ts

import { Component, OnInit } from '@angular/core';
import { User } from './shared/models/user';
import { Router } from '@angular/router';

@Component({
    selector: 'my-app',
    templateUrl: './app/app.component.html',
    styleUrls: ['./app/app.component.css']
})
export class AppComponent implements OnInit {
    ngOnInit() {

    }
}

app.component.html

<router-outlet></router-outlet>

app.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';

export const routes: Routes = [
    {
        path: '',
        redirectTo: '/login',
        pathMatch: 'full',
    },
    {
        path: 'login',
        component: LoginComponent
    }
]

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
like image 244
Leon Gaban Avatar asked Dec 21 '25 07:12

Leon Gaban


2 Answers

I just refactored your project with webpack , your same code works just fine:

github repo

first :

npm install -g @angular/cli

npm install 

ng serve
like image 177
El houcine bougarfaoui Avatar answered Dec 23 '25 19:12

El houcine bougarfaoui


It looks like you might be importing the RouterModule multiple times.

I would remove this line from your app.routing.ts

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

In your app.module I would import your routes with:

import { routes } from './app.routing';

And then import the RouterModule as:

RouterModule.forRoot(routes)
like image 41
Tyler Jennings Avatar answered Dec 23 '25 19:12

Tyler Jennings