gpt4 book ai didi

angular - 没有路由器提供商?

转载 作者:太空狗 更新时间:2023-10-29 16:47:23 27 4
gpt4 key购买 nike

我收到这个错误:

EXCEPTION: Error in ./AppComponent class AppComponent - inlinetemplate:0:0 caused by: No provider for Router!

这是我的应用组件:

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]

但是我得到了这个错误:

Error: Error: No Directive annotation found on RouterModule

我找到了一些示例,但它们使用了已弃用的路由器,而且我没有那个文件夹。我需要那个还是什么?有什么建议吗?

更新:

这是我的应用程序模块:

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

这是我的登录模块:

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 {
}

最佳答案

请使用这个模块

RouterModule.forRoot(
[
{ path: "", component: LoginComponent}
]
)

现在只需更换您的 <login></login><router-outlet></router-outlet>就这样

关于angular - 没有路由器提供商?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42829796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com