Angular2:“...”的路由生成器未包含在传递的参数中
Angular2: Route generator for '...' was not included in parameters passed
我在使用 Angular2 路由时遇到问题。我有几层嵌套。这是我的设置:我为未经身份验证的用户提供顶级路由。登录后,他们位于 /my
路线下。在那个级别,他们有一个仪表板,可以将他们带到 /my/projects/
级别。 projects
下的路由需要一个 ID。设置的详细信息如下。
这是顶级 AppComponent
:
@RouteConfig([
{path: '/home', name:'Home', component: HomeComponent, useAsDefault: true},
{path: '/login', name: 'Login', component: LoginComponent},
{path: '/signup', name: 'SignUp', component:SignUpComponent},
{path: '/my/...', name: 'MyApp', component: MyAppComponent}
])
export class AppComponent {
constructor(private router: Router) {
}
一旦通过身份验证,用户将转到 MyAppComponent,其设置如下:
@RouteConfig([
{path:'/dashboard', name: 'MyDashboard', component: DashboardComponent, useAsDefault: true},
{path: '/projects/...', name: 'MyProjects', component: ProjectRootComponent},
])
export class MyAppComponent {
constructor(private router: Router) {
}
}
仪表板组件:
export class DashboardComponent {
public projects: Array<Project>;
public loaded: boolean;
public loadError: boolean;
constructor(private _service: ProjectService, private router:Router) {
this.projects = [];
this.loaded = false;
}
ngOnInit() {
console.log('Dashboard component initialized');
this._service.getUserProjects()
.then(projects => {
this.loaded = true;
this.projects = projects;
},
error => {
this.loaded = true;
this.loadError = error;
});
}
selectProject(project: Project) {
// this.router.navigateByUrl('/my/projects/' + project.id); // Tried this approach as well with the same error
this.router.parent.navigate(['MyApp', 'MyProjects', {id: project.id}]);
}
在 DashboardComponent 上,我加载了一个项目列表。当我单击一个项目时,将调用 selectProject(project)
方法,该方法应该导航到该项目页面。项目 id
作为参数传入。 MyProjects 路由需要一个 id,设置如下:
@RouteConfig ([
{path: '/:id', name: 'ProjectHome', component: ProjectHomeComponent, useAsDefault: true},
{path: '/:id/details', name: 'ProjectDetails', component: ProjectDetailsComponent },
])
export class ProjectRootComponent implements OnInit {
id: string;
constructor(private router: Router, private routeParams: RouteParams, private projectService: ProjectService) {
}
ngOnInit() {
this.id = this.routeParams.get('id');
console.log('Project Component Initialized with id: ' + this.id);
}
createProject() {
}
}
当仪表板上的 selectProject(project)
被调用时,我收到错误:ORIGINAL EXCEPTION: Route generator for 'id' was not included in parameters passed.
我还尝试使用这两种方法直接导航到项目详细信息:
this.router.parent.navigate(['MyApp', 'MyProjects', {id: project.id}, 'ProjectDetails']
和
this.router.parent.navigate(['MyApp', 'MyProjects', 'ProjectDetails', {id: project.id}]
两者都会产生与上述相同的错误。
注意:这与之前问的 this question 类似,但我的路线已经按照该问题的答案中的建议设置。
我认为您遇到了路线查找问题,因为您是从子路线导航的。来自 RouterLink
docs(linkParams
应该相同):
The first route name should be prepended with /, ./, or ../. If the route begins with /, the router will look up the route from the root of the app. If the route begins with ./, the router will instead look in the current component's children for the route. And if the route begins with ../, the router will look at the current component's parent.
我相信这应该有效:
this.router.navigate(['/MyApp', 'MyProjects', 'ProjectDetails', {id: project.id}]
或从仪表板:
this.router.navigate(['./MyProjects', 'ProjectDetails', {id: project.id}]
我在使用 Angular2 路由时遇到问题。我有几层嵌套。这是我的设置:我为未经身份验证的用户提供顶级路由。登录后,他们位于 /my
路线下。在那个级别,他们有一个仪表板,可以将他们带到 /my/projects/
级别。 projects
下的路由需要一个 ID。设置的详细信息如下。
这是顶级 AppComponent
:
@RouteConfig([
{path: '/home', name:'Home', component: HomeComponent, useAsDefault: true},
{path: '/login', name: 'Login', component: LoginComponent},
{path: '/signup', name: 'SignUp', component:SignUpComponent},
{path: '/my/...', name: 'MyApp', component: MyAppComponent}
])
export class AppComponent {
constructor(private router: Router) {
}
一旦通过身份验证,用户将转到 MyAppComponent,其设置如下:
@RouteConfig([
{path:'/dashboard', name: 'MyDashboard', component: DashboardComponent, useAsDefault: true},
{path: '/projects/...', name: 'MyProjects', component: ProjectRootComponent},
])
export class MyAppComponent {
constructor(private router: Router) {
}
}
仪表板组件:
export class DashboardComponent {
public projects: Array<Project>;
public loaded: boolean;
public loadError: boolean;
constructor(private _service: ProjectService, private router:Router) {
this.projects = [];
this.loaded = false;
}
ngOnInit() {
console.log('Dashboard component initialized');
this._service.getUserProjects()
.then(projects => {
this.loaded = true;
this.projects = projects;
},
error => {
this.loaded = true;
this.loadError = error;
});
}
selectProject(project: Project) {
// this.router.navigateByUrl('/my/projects/' + project.id); // Tried this approach as well with the same error
this.router.parent.navigate(['MyApp', 'MyProjects', {id: project.id}]);
}
在 DashboardComponent 上,我加载了一个项目列表。当我单击一个项目时,将调用 selectProject(project)
方法,该方法应该导航到该项目页面。项目 id
作为参数传入。 MyProjects 路由需要一个 id,设置如下:
@RouteConfig ([
{path: '/:id', name: 'ProjectHome', component: ProjectHomeComponent, useAsDefault: true},
{path: '/:id/details', name: 'ProjectDetails', component: ProjectDetailsComponent },
])
export class ProjectRootComponent implements OnInit {
id: string;
constructor(private router: Router, private routeParams: RouteParams, private projectService: ProjectService) {
}
ngOnInit() {
this.id = this.routeParams.get('id');
console.log('Project Component Initialized with id: ' + this.id);
}
createProject() {
}
}
当仪表板上的 selectProject(project)
被调用时,我收到错误:ORIGINAL EXCEPTION: Route generator for 'id' was not included in parameters passed.
我还尝试使用这两种方法直接导航到项目详细信息:
this.router.parent.navigate(['MyApp', 'MyProjects', {id: project.id}, 'ProjectDetails']
和
this.router.parent.navigate(['MyApp', 'MyProjects', 'ProjectDetails', {id: project.id}]
两者都会产生与上述相同的错误。
注意:这与之前问的 this question 类似,但我的路线已经按照该问题的答案中的建议设置。
我认为您遇到了路线查找问题,因为您是从子路线导航的。来自 RouterLink
docs(linkParams
应该相同):
The first route name should be prepended with /, ./, or ../. If the route begins with /, the router will look up the route from the root of the app. If the route begins with ./, the router will instead look in the current component's children for the route. And if the route begins with ../, the router will look at the current component's parent.
我相信这应该有效:
this.router.navigate(['/MyApp', 'MyProjects', 'ProjectDetails', {id: project.id}]
或从仪表板:
this.router.navigate(['./MyProjects', 'ProjectDetails', {id: project.id}]