angular2中的动态路由
我试图用弹性索引中的索引名创建动态路由。我可以通过手动添加一些名称来创建动态路由,但当我尝试使用弹性索引名称插入路由时,它会跳过该部分 app.routing.tsangular2中的动态路由,angular,
elasticsearch,angular-routing,Angular,
elasticsearch,Angular Routing,我试图用弹性索引中的索引名创建动态路由。我可以通过手动添加一些名称来创建动态路由,但当我尝试使用弹性索引名称插入路由时,它会跳过该部分 app.routing.ts constructor(http: Http, router: Router) { http.get('http://localhost:9200/_stats').subscribe(data => { for (let entry of Object.keys(data.json(
constructor(http: Http, router: Router) {
http.get('http://localhost:9200/_stats').subscribe(data => {
for (let entry of Object.keys(data.json().indices)) {
router.config.push({path: entry, component: SearchComponent});
}
});
router.config.push({path: 'test', component: SearchComponent});
console.log('Routes: ', JSON.stringify(router.config, undefined, 1));
}
我打印了我的路线,结果如下:
Routes: [
{
"path": "home"
},
{
"path": "404/pagenotfound"
},
{
"path": "test"
}
]
我不明白为什么这个代码段跳过了我插入弹性索引名的部分。但当我打印出如下内容时,它显示了我试图添加的索引名
http.get('http://localhost:9200/_stats').subscribe(data => {
for (let entry of Object.keys(data.json().indices)) {
router.config.push({path: entry, component: SearchComponent});
}
console.log('Routes: ', JSON.stringify(router.config, undefined, 1));
});
当我按上述方式打印时得到的结果
Routes: [
{
"path": "home"
},
{
"path": "404/pagenotfound"
},
{
"path": "test"
},
{
"path": "pokemon"
},
{
"path": "cars"
},
{
"path": "tweets"
},
{
"path": "bikes"
},
{
"path": "apple"
}
]
当我试图访问URL时,它抛出一个错误,如下所示
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'tweets'
你能描述一下我做错了什么吗
编辑
import {APP_INITIALIZER, NgModule, OnInit} from '@angular/core';
import {CommonModule,} from '@angular/common';
import {BrowserModule} from '@angular/platform-browser';
import {Routes, RouterModule, Router, Resolve} from '@angular/router';
import {SearchComponent} from './search/search.component';
import {NotFoundComponent} from "./notfoundcomponent/notfoundcomponent.component";
import {HomeComponent} from "./home/home.component";
import {Http} from "@angular/http";
import {init} from "protractor/built/launcher";
export function loadRoutes(http: Http, router: Router) {
return () => {
router.resetConfig([
{path: 'fd', component: HomeComponent}
]);
};
}
let routes: Routes = [
{path: '', redirectTo: 'home', pathMatch: 'full'},
{path: 'home', component: HomeComponent},
{path: '404/pagenotfound', component: NotFoundComponent}
];
@NgModule({
imports: [
CommonModule,
BrowserModule,
RouterModule.forRoot(routes)
],
providers: [{
'provide': APP_INITIALIZER,
'useFactory': init,
'deps': [Http, Router],
'multi': true
}],
entryComponents: [SearchComponent]
})
export class AppRoutingModule {
constructor(private http: Http, private router: Router) {
http.get('http://localhost:9200/_stats').subscribe(data => {
for (let entry of Object.keys(data.json().indices)) {
router.config.push({path: entry, component: SearchComponent});
}
console.log('Routes: ', JSON.stringify(router.config, undefined, 1));
});
console.log('Routes: ', JSON.stringify(router.config, undefined, 1));
}
}
看来你需要使用 另一件我认为应该有帮助的事情是 然后添加加载器
export function loadRoutes(http: Http, router: Router) {
return () => {
//DO your job here
};
}
因此,它将等待加载路由,只有在加载路由之后,它才会继续nop,它不会重新设置路由。但是抛出与
error-error:Uncaught(承诺中)相同的错误:error:无法匹配任何路由。URL段:“tweets”
我按照你说的做了,现在无法构建,这就是我的app.routing类现在的样子<代码>@NgModule({imports:[CommonModule,BrowserModule,RouterModule.forRoot(routes)],提供程序:[{'provide':APP_初始值设定项,'useFactory':init,'deps':[Http,Router],'multi':true}],入口组件:[SearchComponent]})@M.Ramzan用您收到的错误更新您的问题,否则我帮不上忙。我在问题中添加了代码,错误是,C:\test\web\src\child\u进程不存在
@M.Ramzan我糟糕的usefactory应该是“usefactory”:loadRoutes,而不是init
Add to providers
{
'provide': APP_INITIALIZER,
'useFactory': loadRoutes,
'deps': [Http, Router],
'multi': true
},
export function loadRoutes(http: Http, router: Router) {
return () => {
//DO your job here
};