Pra*_*hag 14 angular-routing angular
Angular 2版本:2.0.0-alpha.44
我一直在尝试在Angular 2中进行路由.虽然我能够完成正常的路由,但是当我介绍子路由时,我遇到了一些问题.以下是plnkr上的示例(http://plnkr.co/edit/Y5doqU1WcEe4Ldr7KfPJ)
以下是快速参考的代码.我正在努力实现以下路由
App
/\
/ \
HomeCmp HelloCmp
\
\
ChildCmp
Run Code Online (Sandbox Code Playgroud)
下面是我如何配置我的路径
import {bootstrap, bind, Component, View} from 'angular2/angular2'
import {RouteConfig, RouteParams, ROUTER_DIRECTIVES, APP_BASE_HREF, ROUTER_BINDINGS} from 'angular2/router'
@Component({
selector: 'child-cmp'
})
@View({
template: `
<div>
<h3>Whats up</h3>
</div>
`
})
class ChildCmp { }
// ************************ Hello Component ***********************************
@Component({
selector: 'hello-cmp'
})
@View({
template: `
<div>
<h2>Hello there !</h2>
<router-outlet></router-outlet>
</div>
`,
directives: ROUTER_DIRECTIVES
})
@RouteConfig([
{path: '/', component: ChildCmp, as: 'ChildCmp'}
])
class HelloCmp { }
//************************** HOME Component ***********************************
@Component({
selector: 'home-cmp'
})
@View({
template: `
<div>
<h2>Welcome Home</h2>
</div>
`
})
class HomeCmp {}
//************************* APP Component *************************************
@Component({
selector: 'app-cmp'
})
@View({
template: `
<div>
<h1>Hello {{message}}!</h1>
<a [router-link]="['./HomeCmp']">home</a>
<a [router-link]="['./HelloCmp']">hello</a>
<hr>
<router-outlet></router-outlet>
</div>
`,
directives: ROUTER_DIRECTIVES
})
@RouteConfig([
{path: '/', component: HomeCmp, as: 'HomeCmp'}
{path: '/hello/...', component: HelloCmp, as: 'HelloCmp'}
])
export class App {
message:string = 'world';
}
bootstrap(App, [
ROUTER_BINDINGS,
bind(APP_BASE_HREF).toValue(location.pathname)
]);
Run Code Online (Sandbox Code Playgroud)
当我删除子路线时,它工作正常.但是对于子路线,我会得到以下错误.
EXCEPTION: Link "["HelloCmp"]" does not resolve to a terminal or async instruction. in [null]
Run Code Online (Sandbox Code Playgroud)
我按照这里提到的文章.但是无法让它发挥作用.有人可以帮忙吗?谢谢
Par*_*ain 11
你只需要导入子组件routerLink
然后你的代码工作正常.例如:
<a [routerLink]="['./HelloCmp','ChildCmp']">hello</a>
Run Code Online (Sandbox Code Playgroud)
这是您的代码的工作plnkur. Plunker Code
有关此路由的详细信息请参见GitHub上这个问题在这里
从Angular 2 beta开始,这里有一些变化:
router-link
已更改为routerLink
你也可以useAsDefault : true
在routerLink这样的时候使用而不是提供孩子 -
{path:'/ blabla',components:ABC,name:ABC,useAsDefault:true}
Angular4子路由 -
让我首先定义路由配置,每个路由都可以有一个名为children的属性,您可以在其中定义此路由的子路由.
const routes: Routes = [
{path: '', redirectTo: 'home', pathMatch: 'full'},
{path: 'find', redirectTo: 'search'},
{path: 'home', component: HomeComponent},
{path: 'search', component: SearchComponent},
{
path: 'artist/:artistId',
component: ArtistComponent,
children: [
{path: '', redirectTo: 'tracks'}, ?
{path: 'tracks', component: ArtistTrackListComponent}, ?
{path: 'albums', component: ArtistAlbumListComponent}, ?
]
},
{path: '**', component: HomeComponent}
];
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)<h1>Artist</h1> <p> <a [routerLink]="['./tracks']">Tracks</a> | <a [routerLink]="['./albums']">Albums</a> </p> <router-outlet></router-outlet>
归档时间: |
|
查看次数: |
22311 次 |
最近记录: |