'路由器出口'不是已知元素Angular2

时间:2017-03-08 14:03:14

标签: javascript angular angular2-routing

回购:https://github.com/leongaban/lifeleveler.io

我不知道为什么我收到此错误,我在我的app.component.ts中导入了路由器

我尝试使用app.component来保留主<router-outlet>,并首先提供登录视图。

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);

4 个答案:

答案 0 :(得分:1)

看起来您可能多次导入RouterModule。

我会从您的app.routing.ts

中删除此行
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

app.module我将使用以下网址导入您的路线:

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

然后将RouterModule导入为:

RouterModule.forRoot(routes)

答案 1 :(得分:1)

我刚用webpack重构你的项目,你的代码工作得很好:

github repo

首先:

npm install -g @angular/cli

npm install 

ng serve

答案 2 :(得分:0)

您应该在Model

中添加RouterModule.forRoot(routes)声明
AppModule

并将其从@NgModule({ imports: [ BrowserModule, FormsModule, RouterModule.forRoot(routes) ], declarations: [ AppComponent, LoginComponent ], providers: [ AuthService, ], bootstrap: [ AppComponent ] }) export class AppModule {} 文件中删除:

app.routing.ts

答案 3 :(得分:0)

只需导入RouterOutlet

 $montantTotal =  29.85
 $result = -3.5527136788005E-15