Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular2中的路由 – 链接“[‘Name’]”无法解析为终端指令大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试一个简单的应用程序工作与子路由。

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

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,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',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,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,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',{ path: '/2/',{ path: '/3/',as: 'SubItem3' }
])
export class ChildItem{}


@Component({
    SELEctor: 'home',{ path: '/child/...',as: 'ChildItem' }
])
export class AppComponent {}

bootstrap(AppComponent,{ 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"
  }
}
这可能对其他用户有用,所以我把它写成aswer,评论可能太短。

在您的RouteConfig中,您将ChildItem定义为父路由。 / …部分使它成为父路由,这意味着它有孩子。

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

因此,您不能简单地路由到[‘ChildItem’],而不指定子路由,或者不在路由中添加useAsDefault:true。

所以你有两个选择:

>选项1:在您的一条子路径中添加useAsDefault:true

@RouteConfig([
        { path: '/1',as: 'SubItem1',useAsDefault: truE},as: 'SubItem3' }
])
export class ChildItem{}

使用此选项,每次导航到ChildItem时,它将立即重定向到SubItem1。注意:在以前已经弃用了,坚持命名。

>选项2:在链接中指定一个小孩(你可以通过两种方法来做到这一点)

// 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>

我希望这是有帮助和明确的。

大佬总结

以上是大佬教程为你收集整理的Angular2中的路由 – 链接“[‘Name’]”无法解析为终端指令全部内容,希望文章能够帮你解决Angular2中的路由 – 链接“[‘Name’]”无法解析为终端指令所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。