【发布时间】:2017-03-08 14:03:13
【问题描述】:
回购:https://github.com/leongaban/lifeleveler.io
不知道为什么会出现这个错误,我在 app.component.ts 中导入了路由器
我正在尝试使用app.component 来保存主要的<router-outlet>,并首先提供登录视图。
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);
【问题讨论】:
-
我自己对 Angular2 还是很陌生。我会尝试将
RouterModule添加到您的 app.module NgModule 导入 -
你的代码没问题,但你有这方面的麻烦
-
我刚试了一下,还是一样的错误:(
-
这段代码没有问题。不要导入
routing和RouterModule因为routing与RouterModule相同,但有一些配置 -
你会用新的@angular/cli 解决问题,别担心,这很容易
标签: javascript angular angular2-routing