Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing in Angular2 - Link "['Name']" does not resolve to a terminal instruction

I am trying to get a simple app working with child routing.

When I setup the routes on my child component I get the following error message:

Link "["ChildItem"]" does not resolve to a terminal instruction

If I place all the routes on the parent component it works correctly. If I split the routes between the child and parent components I get the above error.

Here is it working with all routes on the same component:

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

Here is the routes on separate components where I get the error when viewing the 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: `<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"
  }
}
like image 670
John Avatar asked Dec 16 '15 16:12

John


People also ask

What is router link in angular2?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes. Let's explore how to use RouterLink , Router.

How does routing work in Angular?

Overview of Angular RoutingEntering a URL in the address bar and the browser navigates to a corresponding page. Clicking links on the page and the browser navigates to a new page. Clicking the browser's back and forward buttons and the browser navigates backward and forward through the history of pages.


1 Answers

This may be useful for other users, so I'm writing it as an aswer, the comment may be too short.

In your RouteConfig you defined ChildItem to be a parent route. The /... part makes it to be a parent route, which means it has children.

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

So you can't simply route to ['ChildItem'] without specifying a child route or without adding useAsDefault: true in the route.

So you have two options :

  • Option 1 : Add useAsDefault: true in one of your child routes
@RouteConfig([
        { path: '/1', component: SubItem1, as: 'SubItem1', useAsDefault: true},
        { path: '/2/', component: SubItem2, as: 'SubItem2' },
        { path: '/3/', component: SubItem3, as: 'SubItem3' }
])
export class ChildItem{}

With this option, everytime you navigate to ChildItem it will redirect you immediatly to SubItem1. Note : as was deprecated alphas ago, stick to name.

  • Option 2 : Specify a child in the link (you can do this in two ways)
// First way
<a [routerLink]="['/ChildItem', 'SubItem1']">Click Me</a>

// Second way
<a [routerLink]="['/ChildItem/SubItem1']">Click Me</a>

Both ways are similar, but the first one will let you to pass parameters to each route, for example :

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

I hope this was helpful and clear.

like image 86
Eric Martinez Avatar answered Oct 01 '22 19:10

Eric Martinez