简体   繁体   English

Angular2更新为rc5和@ angular / router-原始例外:没有路由器的提供程序

[英]Angular2 updated to rc5 and @angular/router - ORIGINAL EXCEPTION: No provider for Router

I have updated our project from rc4 -> rc5. 我已经从rc4-> rc5更新了我们的项目。 That seems to work fine. 这似乎很好。 At the same time as doing this, i have also updated our code from using the @angular/router-deprecated to @angular/router . 同时,我还使用@angular/router-deprecated@angular/router来更新了我们的代码。

My application is made up of 2 sections, public and protected (public is for all users, protected is just for admins). 我的应用程序由两部分组成,公共部分和受保护部分(公共部分适用于所有用户,受保护的部分仅适用于管理员)。 But currently i just want to get 1 route working, then i can fix the other pages. 但目前我只想让1条路线工作,然后我可以修复其他页面。

As i said i have updated all the code, which i believe is correct and the pages load until i add this to my page [routerLink]="['Home']" 正如我所说的,我已经更新了所有代码,我认为这是正确的,并且页面会一直加载,直到将其添加到我的页面[routerLink]="['Home']"

If i add this, then i get the exception 如果我添加它,那么我会得到例外

ORIGINAL EXCEPTION: No provider for Router! 原来的例外:没有路由器的提供者!

If i comment this line out again, the pages all load fine. 如果我再次对此行发表评论,则所有页面都可以正常加载。

Here is some of my code 这是我的一些代码

app.module.ts app.module.ts

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

import { AppRoutes } from './app.routes';
import { AppComponent }  from './app.component';
import { PublicComponent } from './public.component';
//import { ProtectedComponent } from './protected.component';

// cms
import { HomeComponent } from './cms/home.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        RouterModule.forRoot(AppRoutes)
    ],
    declarations: [
        AppComponent,
        PublicComponent,
        HomeComponent
    ],
    bootstrap: [ AppComponent ]
})

export class AppModule { }

app.routes.ts app.routes.ts

import { PublicComponent } from './public.component';
import { ProtectedComponent } from './protected.component';

// public
import { HomeComponent } from './cms/home.component';


export const AppRoutes = [
    {
        path: '',
        component: PublicComponent,
        children: [
            {
                path: '',
                redirectTo: 'home',
                pathMatch: 'full'
            },
            {
                path: 'home',
                component: HomeComponent
            },
        ]
    },
    // {
    //     path: 'admin/...',
    //     component: ProtectedComponent
    // }
];

app.component.ts app.component.ts

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

@Component({
    selector: 'app-root',
    template: '<router-outlet></router-outlet>',
    directives: [ ROUTER_DIRECTIVES ]
})

export class AppComponent {
    viewContainerRef: ViewContainerRef;

    constructor(viewContainerRef: ViewContainerRef) {
        this.viewContainerRef = viewContainerRef;
    }
}

main.ts 主要

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

public.component.ts public.component.ts

import { Component, OnInit } from '@angular/core';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { CmsService } from './cms/cms.service';
import { ModuleService } from './module/module.service';
import { MenuItem } from './cms/models/menu-item';
import { Settings } from './cms/models/settings';
import { CollapseDirective } from 'ng2-bootstrap/components/collapse';
import { OffClickDirective } from './shared/offclick.directive';

@Component({
    selector: 'public',
    templateUrl: 'app/public.component.html',
    directives: [ ROUTER_DIRECTIVES, CollapseDirective, OffClickDirective ],
    providers: [ CmsService, ModuleService ]
})

export class PublicComponent {
    menu: MenuItem[];
    settings: Settings;
    isCollapsed: boolean = true;

    constructor(
        private cmsService: CmsService,
        private router: Router) {
    }
}

public.component.html public.component.html

<div class="navbar navbar-dark bg-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <a class="navbar-brand" [routerLink]="['Home']">
            <span class="fa fa-home fa-2x" aria-hidden="true"></span>
            <span class="sr-only">Home</span>
        </a>
    </div>
</div>

I have tried adding Router to the providers section in public.component.ts but then i get another exception as below. 我尝试将Router添加到public.component.ts的providers部分,但是随后出现另一个异常,如下所示。

Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?) 错误:无法解析路由器的所有参数:(?,?,?,?,?,?,?,?)

I have obviously missed something, but dont know what. 我显然错过了一些东西,但不知道是什么。 Any help would be much appreciated. 任何帮助将非常感激。

Just encase it helps, here are the angular references in my package.json file as well 只是包裹一下就可以了,这也是我package.json文件中的角度引用

"dependencies": {
    "@angular/common": "^2.0.0-rc.5",
    "@angular/compiler": "^2.0.0-rc.5",
    "@angular/core": "^2.0.0-rc.5",
    "@angular/forms": "^0.3.0",
    "@angular/http": "^2.0.0-rc.5",
    "@angular/platform-browser": "^2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "^2.0.0-rc.5",
    "@angular/router": "^3.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "^2.0.0-rc.3"
}
  1. Delete routermodule from app.module.ts 从app.module.ts删除routermodule
  2. Adjust app.module.ts 调整app.module.ts

     ... import { routing, appRoutingProviders } from './app.routing'; ... @NgModule({ imports: [ ... routing, ... ], ... providers: [ ... appRoutingProviders, ... ], bootstrap: [AppComponent], }) export class AppModule { } 
  3. Adjust app.routing.ts: 调整app.routing.ts:

     import { Routes, RouterModule } from '@angular/router'; import { DashboardComponent } from './dashboard/dashboard.component'; ... import { Http, HTTP_PROVIDERS } from '@angular/http'; const appRoutes: Routes = [ { path: '', redirectTo: '/dashboard', pathMatch: 'full'}, { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard]}, ... { path: '**', redirectTo: 'dashboard', pathMatch: 'full'} ]; export const appRoutingProviders: any[] = [ ... Http, HTTP_PROVIDERS ]; export const routing = RouterModule.forRoot(appRoutes); 
  4. Remove ROUTER_DIRECTIVES everywhere 随处删除ROUTER_DIRECTIVES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM