我使用 Angular2 alpha39和Babel 来转换ES6 JS文件。我没有使用打字稿。
我创建了一个正确显示的组件。我在模板中添加了 router-outlet 。当我运行应用程序时,收到错误消息:
没有路由器提供商! (RouterOutlet - >路由器)
调用堆栈是:
以下是代码片段:
模板:
.... // Removed for brevity
<div class="contenttext">
<router-outlet></router-outlet>
</div>
.... // Removed for brevity
组件文件:
import { Component, View, bootstrap, OnInit } from 'angular2/angular2';
import { RouteConfig, RouterOutlet, RouterLink } from 'angular2/router';
import 'reflect-metadata';
import 'winjs';
@Component({
selector: 'dashboard-app'
})
@View({
templateUrl: '../js/dashboard.html',
directives: [ ContentComponent, FamiliesComponent, RouterOutlet, RouterLink ]
})
@RouteConfig([
{ path: '/employees', component: EmployeesComponent, as: 'employees'}
])
class DashboardAppComponent implements OnInit {
constructor() {
}
onInit() {
WinJS.UI.processAll().done(function() {
var splitView = document.querySelector(".splitView").winControl;
new WinJS.UI._WinKeyboard(splitView.paneElement);
})
}
}
bootstrap(DashboardAppComponent);
答案 0 :(得分:37)
你必须使用:
我希望这肯定能帮助你。
在释放alpha41之后:
ROUTER_BINDINGS
已更改为ROUTER_PROVIDERS
。 ROUTER_DIRECTIVES
。Router-link
期望该值为路由名称数组。了解更多信息。参考here。有关路由的更多信息,请参阅本教程here。
(根据alpha-47所有生命周期钩子重命名为。)
onActivate,onReuse,onDeactivate,canReuse,canDeactivate
要: -
routerOnActivate,routerOnReuse,routerOnDeactivate,routerCanReuse,routerCanDeactivate
router-link
已更改为routerLink
并将routeconfig属性更改为:
{path: '/abc', component: ABC, as: 'abc'}
至:
{path: '/xyz' , component: XYZ, name: 'xyz'}
在RC中的路由中有很多变化在RC之后,其中一些点我会提到这里可能会帮助某人: -
angular2/router
已更改@angular/router
(您也可以使用导入@angular/router-deprecated
来使用路由的旧功能,但到目前为止我们必须使用@angular/router
)。
@RouteConfig
已更改为@Routes
。
例如: -
@Routes([
{path: '/crisis-center', component: CrisisListComponent},
{path: '/heroes', component: HeroListComponent}
])
答案 1 :(得分:9)
<强> 2.0.0-alpha.36 (2015-08-31)
强>
routerInjectables
已重命名为ROUTER_BINDINGS
<强> 2.0.0-alpha.41 (2015-10-13)
强>
ROUTER_BINDINGS
已重命名为ROUTER_PROVIDERS
使用ROUTER_PROVIDERS
ROUTER_PROVIDERS
用于简化路由器的引导。
它包括:
RouterRegistry
- 已注册路线的集合LocationStrategy = PathLocationStrategy
- 按路径匹配 ROUTER_PROVIDERS
提供“理智”默认值,除非您需要不同的路线LocationStrategy
,否则应使用默认值。
变化:
bootstrap(DashboardAppComponent);
到:
bootstrap(DashboardAppComponent, [
ROUTER_PROVIDERS
]);
来源:
<强> 2.0.0-alpha.38 (2015-10-03)
强>
路由别名需要是CamelCase(技术上是PascalCase)
注意:这已经在Pardeep的#3
的答案中提到过如果要通过router-link
在模板中包含指向路径的链接,则必须确保路径的别名(即name
属性)为PascalCase。
如果您使用计划使用router-link
修改路线:
{ path: '/employees', component: EmployeesComponent, name: 'Employees'}
然后,您可以在模板中添加以下链接:
<a [router-link]="['/Employees']">Employees Link</a>
RouterLink
动态插入与路径路径匹配的href。
注意:阅读问题/ pr看起来此更改是为了防止用户将<route-link>
绑定与路由网址混淆
来源:
提示:
如果您想简化视图指令,请使用ROUTER_DIRECTIVES
它包括:
RouterLink
RouterOutlet
<强>更新强>
在不久的将来,RouterOutlet
/ <router-outlet>
将重命名为RouterViewport
/ <router-viewport>
来源:
更新2:
RouteConfig
属性as
已重命名为name
来源:
答案 2 :(得分:4)
回答 2016年12月23日(Angular v2.4.1,路由器v3.4.1 - 适用于任何NG v2.x.x +路由器v3.x.x)
我刚刚将三个应用程序从Webpack Starter Seed迁移到 Angular CLI (v1.0.0-beta.24)并遇到了此问题。
只需要NG 2 massive router doc page上的一小部分内容:
app-routing.module.ts文件(通常在src / app /文件夹中),如下所示:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const appRoutes: Routes = [
{ path: '', component: YourHomePageComponent },
{ path: 'next-page', component: NextComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
将AppRoutingModule 导入主模块(通常是src / app / app.module.ts):
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule // <--- The import you need to add
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
确保您的主html(通常为src / app / app.component.html)中的某处<router-outlet></router-outlet>
,因为这是注入路由器内容的地方。
答案 3 :(得分:3)
确保在AppModule中定义并声明了路由器。 示例(查看提到路由的所有位置,忽略其余部分):
app.routing.ts
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HeroesComponent } from './heroes.component';
import {DashboardComponent} from './dashboard.component';
import {HeroDetailComponent} from './hero-detail.component';
const appRoutes: Routes = [
{
path: 'heroes',
component: HeroesComponent
},
{
path: 'dashboard',
component: DashboardComponent
},
{
path: '',
redirectTo: '/dashboard',
pathMatch: 'full'
},
{
path: 'detail/:id',
component: HeroDetailComponent
},
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
和app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroService } from './hero.service';
import { routing } from './app.routing';
import './rxjs-extensions';
import {HeroSearchComponent} from './hero-search.component';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
],
declarations: [
AppComponent,
DashboardComponent,
HeroDetailComponent,
HeroesComponent,
HeroSearchComponent
],
providers: [
HeroService,
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}
答案 4 :(得分:1)
这可以节省一个小时:
如果你甚至不使用路由,你会得到这个错误(例如临时,也许你没有导入路由配置和路由器插座被注释掉)但是你在一些组件构造函数中通过依赖注入使用Router或ActivatedRoute ,像这样:
@Component({...}})
export class SomeComponent {
constructor(private _router: Router, private _route: ActivatedRoute) {
//may be you are not using _route/_route at the moment at all!
}
...
}
答案 5 :(得分:1)
如果您没有定义任何路径,则无法为Router
使用依赖注入!
要为路由用户定义类似于以下代码的内容:
const loginRoutes: Routes = [
{path: 'foo/bar/baz', component: 'MyRootComponent'}
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule,
RouterModule.forRoot(loginRoutes)
],
providers: [],
declarations: [
MyLoginComponent
],
bootstrap: [
MyLoginComponent
]
})
export class MyLoginModule
{
}