当前位置:  开发笔记 > 编程语言 > 正文

Angular2中的路由 - 链接"['Name']"不解析为终端指令

如何解决《Angular2中的路由-链接"['Name']"不解析为终端指令》经验,为你挑选了1个好方法。

我正在尝试使用子路由进行简单的应用程序.

当我在我的子组件上设置路由时,我收到以下错误消息:

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: `

Child Item

` }) export class ChildItem{} @Component({ selector: 'home', directives: [COMMON_DIRECTIVES, CORE_DIRECTIVES, ROUTER_DIRECTIVES, RouterLink], template: `

Home page

Click Me` }) export class Home{} @Component({ selector: 'my-app', template: `

Routing Test

`, 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 })])

以下是单独组件上的路由,在查看Child Compoent时出现错误:

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: `

Child Item

` }) @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: `

Home page

Click Me` }) export class Home{} @Component({ selector: 'my-app', template: `

Routing Test

`, 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


  
    Child Routing
    
    
    
    
    
    
    
    
  
  
    Loading...
  

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"
  }
}

Eric Martine.. 55

这可能对其他用户有用,所以我把它写成aswer,评论可能太短了.

RouteConfig你的定义ChildItem为父路线.该/...部分使其成为父路线,这意味着它有子女.

@RouteConfig([
        // This route is a parent route
        { path: '/child/...', component: ChildItem, as: 'ChildItem' }
])

因此,如果['ChildItem']不指定子路由或不添加路由,则无法简单地路由到useAsDefault: true.

所以你有两个选择:

选项1:添加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已弃用alphas,坚持name.

选项2:在链接中指定子项(您可以通过两种方式执行此操作)

// First way
Click Me

// Second way
Click Me

两种方式都相似,但第一种方法可以让您将参数传递给每个路径,例如:

// ChildItem gets "id"
// SubItem1 gets "itemName"
Click Me

// Only SubItem1 gets "id"
Click Me

我希望这有用而且清楚.



1> Eric Martine..:

这可能对其他用户有用,所以我把它写成aswer,评论可能太短了.

RouteConfig你的定义ChildItem为父路线.该/...部分使其成为父路线,这意味着它有子女.

@RouteConfig([
        // This route is a parent route
        { path: '/child/...', component: ChildItem, as: 'ChildItem' }
])

因此,如果['ChildItem']不指定子路由或不添加路由,则无法简单地路由到useAsDefault: true.

所以你有两个选择:

选项1:添加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已弃用alphas,坚持name.

选项2:在链接中指定子项(您可以通过两种方式执行此操作)

// First way
Click Me

// Second way
Click Me

两种方式都相似,但第一种方法可以让您将参数传递给每个路径,例如:

// ChildItem gets "id"
// SubItem1 gets "itemName"
Click Me

// Only SubItem1 gets "id"
Click Me

我希望这有用而且清楚.


优秀!这实际上也是1.5组件路由器的情况.
推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有