gpt4 book ai didi

Angular 2 : Can't resolve all parameters for Router

转载 作者:太空狗 更新时间:2023-10-29 17:13:48 25 4
gpt4 key购买 nike

目标

在不失去理智的情况下让路由正常工作。

错误

Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?)

app.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

export const appRoutes: Routes = [
{ path: '', redirectTo: 'customers', pathMatch: 'full' },
{ path: 'customers', component: CustomerComponent },
];

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

app.module.ts

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

import { routing } from './app.routing';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

@NgModule({
imports: [
BrowserModule,
FormsModule,
routing
],
declarations: [
AppComponent,
NavbarComponent,
CustomerComponent,
],
providers: [
// ...
],
bootstrap: [ AppComponent ]
})
export class AppModule {
// ...
}

app.component.ts

import { RouterLink } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';
import { Component } from '@angular/core';

import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

export { Config } from './config/env.config';

@Component({
moduleId: module.id,
selector: 'app',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css'],
directives: [NavbarComponent, CustomerComponent],
providers: [HTTP_PROVIDERS, RouterLink]
})
export class AppComponent
{
constructor() {
// console.log('Environment config', Config);
}

ngOnInit() {
// ...
}
}

navbar.component.ts

import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { Component } from '@angular/core';

@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: 'navbar.component.html',
styleUrls: ['navbar.component.css'],
directives: [ROUTER_DIRECTIVES],
providers: [Router],
})
export class NavbarComponent
{
version: string;
versionIsVisible: boolean;

constructor() {
this.version = '<%= VERSION %>';
}

ngOnInit() {
// ...
}
}

app.component.html

<navbar></navbar>

<router-outlet></router-outlet>

navbar.component.html

<a routerLink="/customers">Customers</a>

最佳答案

现在感谢旧帖子,但我遇到了同样的问题并且刚刚解决了它。

该错误提示 Angular 无法解析 Router 类的某些依赖项。

navbar.component.ts

import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { Component } from '@angular/core';

@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: 'navbar.component.html',
styleUrls: ['navbar.component.css'],
directives: [ROUTER_DIRECTIVES],
providers: [Router],
})

我通过不将 Router 作为提供者注入(inject),而只注入(inject)构造函数来解决此问题,因此您最终得到:

@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: 'navbar.component.html',
styleUrls: ['navbar.component.css'],
directives: [ROUTER_DIRECTIVES]
})

export class NavbarComponent
{
version: string;
versionIsVisible: boolean;

constructor(private _router: Router) {
this.version = '<%= VERSION %>';
}

ngOnInit() {
// ...
}
}

我是 angular2 的新手,所以我不能给你一个详细的原因!

关于 Angular 2 : Can't resolve all parameters for Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39372979/

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