我已经从rc4更新了我们的项目 - > RC5。这似乎工作正常。
在执行此操作的同时,我还使用@angular/router-deprecated
到@angular/router
更新了我们的代码。
我的应用程序由2个部分组成,包括public和protected(所有用户都是公共的,受保护的只是管理员)。但是目前我只想让1条路线工作,然后我可以修复其他页面。
正如我所说,我已经更新了所有代码,我相信这是正确的,并且页面会加载,直到我将其添加到我的页面[routerLink]="['Home']"
如果我添加这个,那么我得到异常
ORIGINAL EXCEPTION:没有路由器的提供商!
如果我再次评论此行,则所有页面都可以正常加载。
以下是我的一些代码
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
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
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
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
<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>
我尝试将Router
添加到public.component.ts
中的提供者部分,但接下来又出现了另一个例外情况。
我显然错过了什么,但不知道是什么。任何帮助将不胜感激。错误:无法解析路由器的所有参数:(?,?,?,?,?,?,?, ?)
只是包装它有帮助,这里是我的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"
}
答案 0 :(得分:0)
调整app.module.ts
...
import { routing, appRoutingProviders } from './app.routing';
...
@NgModule({
imports: [
...
routing,
...
],
...
providers: [
...
appRoutingProviders,
...
],
bootstrap: [AppComponent],
})
export class AppModule { }
调整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);
随处删除ROUTER_DIRECTIVES