Angular2 中的路由 - Link“['Name']”不解析为终端指令
Routing in Angular2 - Link "['Name']" does not resolve to a terminal instruction
我正在尝试获得一个使用子路由的简单应用程序。
当我在我的子组件上设置路由时,我收到以下错误消息:
Link "["ChildItem"]" does not resolve to a terminal instruction
如果我将所有路由放在父组件上,它就可以正常工作。如果我在子组件和父组件之间拆分路由,则会出现上述错误。
这是在同一个组件上处理所有路由:
import {bootstrap} from 'angular2/platform/browser';
import {Component, provide} from 'angular2/core';
import {COMMON_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgIf} from 'angular2/common';
import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouterLink, RouterOutlet, Route, LocationStrategy, HashLocationStrategy} from 'angular2/router';
@Component({selector: 'subItem1',template: `SubItem1`})export class SubItem1{}
@Component({selector: 'subItem2',template: `SubItem2`})export class SubItem2{}
@Component({selector: 'subItem3',template: `SubItem3`})export class SubItem3{}
@Component({
selector: 'childItem',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Child Item</h2>
<ul>
<li><a [routerLink]="['/SubItem1']">SubItem1</a></li>
<li><a [routerLink]="['/SubItem2']">SubItem2</a></li>
<li><a [routerLink]="['/SubItem3']">SubItem3</a></li>
</ul>
`
})
export class ChildItem{}
@Component({
selector: 'home',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Home page</h2>
<a [routerLink]="['/ChildItem']">Click Me</a>`
})
export class Home{}
@Component({
selector: 'my-app',
template: `<h1>Routing Test</h1>
<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES, RouterLink, RouterOutlet]
})
@RouteConfig([
{ path: '/', component: Home, as: 'Home' },
{ path: '/child', component: ChildItem, as: 'ChildItem' },
{ path: '/child/1/', component: SubItem1, as: 'SubItem1' },
{ path: '/child/2/', component: SubItem2, as: 'SubItem2' },
{ path: '/child/3/', component: SubItem3, as: 'SubItem3' }
])
export class AppComponent {}
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })])
这是单独组件上的路由,在查看子组件时出现错误:
import {bootstrap} from 'angular2/platform/browser';
import {Component, provide} from 'angular2/core';
import {COMMON_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgIf} from 'angular2/common';
import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouterLink, RouterOutlet, Route, LocationStrategy, HashLocationStrategy} from 'angular2/router';
@Component({selector: 'subItem1',template: `SubItem1`})export class SubItem1{}
@Component({selector: 'subItem2',template: `SubItem2`})export class SubItem2{}
@Component({selector: 'subItem3',template: `SubItem3`})export class SubItem3{}
@Component({
selector: 'childItem',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Child Item</h2>
<ul>
<li><a [routerLink]="['/SubItem1']">SubItem1</a></li>
<li><a [routerLink]="['/SubItem2']">SubItem2</a></li>
<li><a [routerLink]="['/SubItem3']">SubItem3</a></li>
</ul>
`
})
@RouteConfig([
{ path: '/1', component: SubItem1, as: 'SubItem1' },
{ path: '/2/', component: SubItem2, as: 'SubItem2' },
{ path: '/3/', component: SubItem3, as: 'SubItem3' }
])
export class ChildItem{}
@Component({
selector: 'home',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Home page</h2>
<a [routerLink]="['/ChildItem']">Click Me</a>`
})
export class Home{}
@Component({
selector: 'my-app',
template: `<h1>Routing Test</h1>
<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES, RouterLink, RouterOutlet]
})
@RouteConfig([
{ path: '/', component: Home, as: 'Home' },
{ path: '/child/...', component: ChildItem, as: 'ChildItem' }
])
export class AppComponent {}
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })])
Index.html
<html>
<head>
<title>Child Routing</title>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="styles.css">
<script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="../node_modules/angular2/bundles/router.dev.js"></script>
<script src="../node_modules/rxjs/bundles/rx.js"></script>
<script>
System.config({
packages: {'app': {defaultExtension: 'js'}},
baseURL: '/src'
});
System.import('app/app');
</script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
packages.json
{
"name": "angular2-forms",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"tsc": "tsc -p src -w",
"start": "live-server --open=src"
},
"keywords": [],
"author": "John Tindell",
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.0",
"es6-module-loader": "0.17.8",
"systemjs": "0.19.8",
"es6-shim":"0.33.3",
"rxjs": "5.0.0-beta.0"
},
"devDependencies": {
"live-server": "^0.9.0",
"typescript": "^1.7.3"
}
}
这可能对其他用户有用,所以我把它写成一个答案,评论可能太短了。
在您的 RouteConfig
中,您将 ChildItem
定义为 parent 路线。 /...
部分使它成为一条 parent 路线,这意味着它有 children.
@RouteConfig([
// This route is a parent route
{ path: '/child/...', component: ChildItem, as: 'ChildItem' }
])
所以你不能在不指定 child 路由或不在路由中添加 useAsDefault: true
的情况下简单地路由到 ['ChildItem']
。
所以你有两个选择:
- 选项 1:在您的 child 路线之一中添加
useAsDefault: true
@RouteConfig([
{ path: '/1', component: SubItem1, as: 'SubItem1', useAsDefault: true},
{ path: '/2/', component: SubItem2, as: 'SubItem2' },
{ path: '/3/', component: SubItem3, as: 'SubItem3' }
])
export class ChildItem{}
使用此选项,每次您导航到 ChildItem
时,它都会立即将您重定向到 SubItem1
。 注意:as
在 alpha 之前已被弃用,坚持使用 name
。
- 选项2 : 在link中指定一个child(你可以通过两种方式做到这一点)
// First way
<a [routerLink]="['/ChildItem', 'SubItem1']">Click Me</a>
// Second way
<a [routerLink]="['/ChildItem/SubItem1']">Click Me</a>
两种方式都差不多,但是第一种会让你给每条路由传递参数,例如:
// ChildItem gets "id"
// SubItem1 gets "itemName"
<a [routerLink]="['/ChildItem', {id: 'someId'}, 'SubItem1', {itemName: 'someName'}]">Click Me</a>
// Only SubItem1 gets "id"
<a [routerLink]="['/ChildItem/SubItem1', {id: 'someId'}]">Click Me</a>
我希望这对您有所帮助且清晰。
我正在尝试获得一个使用子路由的简单应用程序。
当我在我的子组件上设置路由时,我收到以下错误消息:
Link "["ChildItem"]" does not resolve to a terminal instruction
如果我将所有路由放在父组件上,它就可以正常工作。如果我在子组件和父组件之间拆分路由,则会出现上述错误。
这是在同一个组件上处理所有路由:
import {bootstrap} from 'angular2/platform/browser';
import {Component, provide} from 'angular2/core';
import {COMMON_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgIf} from 'angular2/common';
import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouterLink, RouterOutlet, Route, LocationStrategy, HashLocationStrategy} from 'angular2/router';
@Component({selector: 'subItem1',template: `SubItem1`})export class SubItem1{}
@Component({selector: 'subItem2',template: `SubItem2`})export class SubItem2{}
@Component({selector: 'subItem3',template: `SubItem3`})export class SubItem3{}
@Component({
selector: 'childItem',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Child Item</h2>
<ul>
<li><a [routerLink]="['/SubItem1']">SubItem1</a></li>
<li><a [routerLink]="['/SubItem2']">SubItem2</a></li>
<li><a [routerLink]="['/SubItem3']">SubItem3</a></li>
</ul>
`
})
export class ChildItem{}
@Component({
selector: 'home',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Home page</h2>
<a [routerLink]="['/ChildItem']">Click Me</a>`
})
export class Home{}
@Component({
selector: 'my-app',
template: `<h1>Routing Test</h1>
<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES, RouterLink, RouterOutlet]
})
@RouteConfig([
{ path: '/', component: Home, as: 'Home' },
{ path: '/child', component: ChildItem, as: 'ChildItem' },
{ path: '/child/1/', component: SubItem1, as: 'SubItem1' },
{ path: '/child/2/', component: SubItem2, as: 'SubItem2' },
{ path: '/child/3/', component: SubItem3, as: 'SubItem3' }
])
export class AppComponent {}
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })])
这是单独组件上的路由,在查看子组件时出现错误:
import {bootstrap} from 'angular2/platform/browser';
import {Component, provide} from 'angular2/core';
import {COMMON_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgIf} from 'angular2/common';
import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouterLink, RouterOutlet, Route, LocationStrategy, HashLocationStrategy} from 'angular2/router';
@Component({selector: 'subItem1',template: `SubItem1`})export class SubItem1{}
@Component({selector: 'subItem2',template: `SubItem2`})export class SubItem2{}
@Component({selector: 'subItem3',template: `SubItem3`})export class SubItem3{}
@Component({
selector: 'childItem',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Child Item</h2>
<ul>
<li><a [routerLink]="['/SubItem1']">SubItem1</a></li>
<li><a [routerLink]="['/SubItem2']">SubItem2</a></li>
<li><a [routerLink]="['/SubItem3']">SubItem3</a></li>
</ul>
`
})
@RouteConfig([
{ path: '/1', component: SubItem1, as: 'SubItem1' },
{ path: '/2/', component: SubItem2, as: 'SubItem2' },
{ path: '/3/', component: SubItem3, as: 'SubItem3' }
])
export class ChildItem{}
@Component({
selector: 'home',
directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink],
template: `<h2>Home page</h2>
<a [routerLink]="['/ChildItem']">Click Me</a>`
})
export class Home{}
@Component({
selector: 'my-app',
template: `<h1>Routing Test</h1>
<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES, RouterLink, RouterOutlet]
})
@RouteConfig([
{ path: '/', component: Home, as: 'Home' },
{ path: '/child/...', component: ChildItem, as: 'ChildItem' }
])
export class AppComponent {}
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })])
Index.html
<html>
<head>
<title>Child Routing</title>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="styles.css">
<script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="../node_modules/angular2/bundles/router.dev.js"></script>
<script src="../node_modules/rxjs/bundles/rx.js"></script>
<script>
System.config({
packages: {'app': {defaultExtension: 'js'}},
baseURL: '/src'
});
System.import('app/app');
</script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
packages.json
{
"name": "angular2-forms",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"tsc": "tsc -p src -w",
"start": "live-server --open=src"
},
"keywords": [],
"author": "John Tindell",
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.0",
"es6-module-loader": "0.17.8",
"systemjs": "0.19.8",
"es6-shim":"0.33.3",
"rxjs": "5.0.0-beta.0"
},
"devDependencies": {
"live-server": "^0.9.0",
"typescript": "^1.7.3"
}
}
这可能对其他用户有用,所以我把它写成一个答案,评论可能太短了。
在您的 RouteConfig
中,您将 ChildItem
定义为 parent 路线。 /...
部分使它成为一条 parent 路线,这意味着它有 children.
@RouteConfig([
// This route is a parent route
{ path: '/child/...', component: ChildItem, as: 'ChildItem' }
])
所以你不能在不指定 child 路由或不在路由中添加 useAsDefault: true
的情况下简单地路由到 ['ChildItem']
。
所以你有两个选择:
- 选项 1:在您的 child 路线之一中添加
useAsDefault: true
@RouteConfig([
{ path: '/1', component: SubItem1, as: 'SubItem1', useAsDefault: true},
{ path: '/2/', component: SubItem2, as: 'SubItem2' },
{ path: '/3/', component: SubItem3, as: 'SubItem3' }
])
export class ChildItem{}
使用此选项,每次您导航到 ChildItem
时,它都会立即将您重定向到 SubItem1
。 注意:as
在 alpha 之前已被弃用,坚持使用 name
。
- 选项2 : 在link中指定一个child(你可以通过两种方式做到这一点)
// First way
<a [routerLink]="['/ChildItem', 'SubItem1']">Click Me</a>
// Second way
<a [routerLink]="['/ChildItem/SubItem1']">Click Me</a>
两种方式都差不多,但是第一种会让你给每条路由传递参数,例如:
// ChildItem gets "id"
// SubItem1 gets "itemName"
<a [routerLink]="['/ChildItem', {id: 'someId'}, 'SubItem1', {itemName: 'someName'}]">Click Me</a>
// Only SubItem1 gets "id"
<a [routerLink]="['/ChildItem/SubItem1', {id: 'someId'}]">Click Me</a>
我希望这对您有所帮助且清晰。