【发布时间】:2018-02-28 11:32:06
【问题描述】:
问题:我正在为我的应用程序配置路由。我想让我的 url 像 https://localhost:4200/hero=id 一样,其中 id 将是用户从 Heroscomponent 中选择的内容。这对我不起作用。
如果我尝试下面的 url,其路径是 /hero/:id,根据 angular 文档,它在音素上起作用。
https://localhost:4200/hero/:id
有人能帮我解决这个问题吗?
这是我的路由配置文件
const appRoutes: Routes = [
{ path: 'hero', component: HeroesComponent },
{path: 'hero{=:id}', component: HeroDetailComponent},
{
path: 'home',
redirectTo: '/hero',
data: { title: 'Heroes List' }
},{
path: 'student',
component: AppTable
},{
path: 'video',
component: VideoTagComponent
},{ path: '',
redirectTo: '/hero',
pathMatch: 'full'
}
// { path: '**', component: PageNotFoundComponent }
];
下面是我要路由到 path = "/hero="+id
的 HeroesComponent 文件import { Component } from '@angular/core';
import { Router } from '@angular/router';
import {Hero} from './hero';
const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
@Component({
selector: 'my-heroes',
templateUrl: './heroes.html',
styleUrls: ['./app.component.css']
})
export class HeroesComponent {
hero = HEROES;
path:string;
selectedHero: Hero;
constructor(private router: Router){}
onSelect(hero: Hero): void {
this.selectedHero = hero;
this.path = "/hero=" +this.selectedHero.id.toString();
this.router.navigate([this.path]);
}
// gotoDetail(): void {
// }
}
这是我在浏览器控制台中遇到的错误。core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'hero%3D13'
Error: Cannot match any routes. URL Segment: 'hero%3D13'**strong text**
【问题讨论】:
-
id 路由使用“=”是一个要求吗?
-
是的@JayDeeEss 这是一个要求。
标签: javascript angular typescript angular4-router