最佳答案
我得到了这个错误:
例外: ./AppComponent 类 AppComponent-inline 中的错误 模板: 0:0造成: 没有路由器提供商!
这是我的应用程序组件:
import {Component} from '@angular/core';
import {LoginComponent} from './login/components/login.component';
@Component({
selector: 'my-app',
template: '<login></login>',
})
export class AppComponent {
}
我在我的应用程序模块中尝试了这个方法:
import { RouterModule } from '@angular/router';
imports: [
BrowserModule,
FormsModule,
HttpModule,
LoginModule,
RouterModule
],
bootstrap: [AppComponent, RouterModule]
但是我得到了这个错误:
错误: 错误: 在 RouterModule 上没有找到指令注释
我发现一些例子,但他们使用路由器-弃用,我没有这个文件夹。我需要这个吗?有什么建议吗?
更新:
这是我的 app.module:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { LoginModule } from './login/login.module';
import { RouterModule } from '@angular/router';
//import 'rxjs/Rx';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
LoginModule,
RouterModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
应用程式组件:
import {Component} from '@angular/core';
import {LoginComponent} from './login/components/login.component';
@Component({
selector: 'my-app',
template: '<login></login>',
})
export class AppComponent {
}
然后我有 login.Component:
import {Component, EventEmitter, Input, OnChanges} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import { LoginService } from '../services/login.service';
import { Login } from '../model/login'
import {Router} from '@angular/router';
@Component({
selector: 'login',
templateUrl: 'login-form',
})
export class LoginComponent {
// Constructor with injected service
constructor(
private loginService: LoginService,
private router: Router
){}
submitLogin(values){
// Variable to hold a reference of addComment/updateComment
let loginOperation:Observable<any>;
loginOperation = this.loginService.Login(values);
loginOperation.subscribe(
function(response) { console.log("Success Response" + response)},
function(error) { console.log("Error happened" + error)},
function() {
console.log("the subscription is completed");
this.router.navigate(['/About']);
}
);
}
}
也许问题出在这一行:
this.router.navigate(['/home']);
这是我的 login.module:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule, JsonpModule } from '@angular/http';
import { LoginComponent } from './components/login.component';
import { RouterModule } from '@angular/router';
import { LoginService } from './services/login.service';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule,
RouterModule
],
declarations: [
LoginComponent
],
providers: [
LoginService
],
exports:[
LoginComponent
]
})
export class LoginModule {
}