【发布时间】:2017-08-30 22:37:09
【问题描述】:
Angular 2 版本:2.0.0-alpha.44
我一直在尝试在 Angular 2 中进行路由。虽然我能够完成正常的路由,但是当我引入子路由时我遇到了一些问题。这是 plnkr (http://plnkr.co/edit/Y5doqU1WcEe4Ldr7KfPJ) 上的示例
下面是快速参考的代码。我正在尝试实现以下路由
App
/\
/ \
HomeCmp HelloCmp
\
\
ChildCmp
下面是我如何配置我的路径
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)
]);
当我删除子路由时,它工作正常。但是对于子路线,我会遇到以下错误。
EXCEPTION: Link "["HelloCmp"]" does not resolve to a terminal or async instruction. in [null]
我关注了here提到的文章。 但无法使其正常工作。有人可以帮忙吗?谢谢
【问题讨论】: