4

I am experimenting with Angular2 alpha35, using TypeScript 6.0.0. My index.html loads a headerFooter component. I then added router-outlet tags to it to insert several pages in between. The code at How to change router by typescript in angular2? helped to eliminate several errors, but after many hours of thrashing, I still get a TypeScript error in the @RouteConfig, namely ',' expected. at . . . ( the : right after component), and a Console error of unexpected @ token in the partial page component that it finds (the GET home.js is ok), but cannot load/insert. Here's relevant code. app.ts, index.html, and home.js are all in the same directory.

index.html

<residence-app><h1>Loading . . .</h1></residence-app>

app.ts

/// <reference path="../angular2-oPost/typings/angular2/angular2.d.ts" />
"use strict";
import { Component, View, bootstrap, Directive } from "angular2/angular2";
import { routerInjectables, routerDirectives, Router, RouterOutlet, RouteConfig, RouterLink } from 'angular2/router';
import { Home } from "home";
@Component({
  selector: 'residence-app'
})
@View({
  directives: [ RouterOutlet, RouterLink, Home ],
  templateUrl: "components/navigation/headerFooter.html",
  styleUrls: ['commonStyles/headerFooter.css']
})
@RouteConfig( [  {path: '/home', as:  component: Home } ] )
// TypeScript error  ',' expected.  at line 32 col48 ( : right after component), but a , causes 2 other errors
class ResidenceApp {
}
bootstrap( ResidenceApp,
  [
    routerInjectables
  ] );

headerFooter.html has several divs and the following tag to insert partial page components

<router-outlet></router-outlet>

home.js is the home page test component to insert in router-outlet in headerFooter.html

"use strict";
import { Component, View, bootstrap, Directive } from "angular2/angular2";
@Component({
//Error loading "home" from "app" at http://localhost/angular2-oPost/app.js
//http://localhost/angular2-oPost/home.js:3:1: Unexpected token @ (WARNING: non-Error used)"
  selector: "home"
})
@View({
  template: `<h1>Home page under construction</h1>
  <a router-link='home'>Home Page</a>`
})
class Home {
}
bootstrap( Home );
Community
  • 1
  • 1
Mike_Laird
  • 1,124
  • 4
  • 16
  • 40
  • In this line `{path: '/home', as: component: Home }` you are missing the `as:`. It should be something like this : `{path: '/home', as: 'alias', component: Home }`. And your second error is most likely a configuration issue since it doesn't recognize the annotations. – Eric Martinez Sep 02 '15 at 00:32
  • Eric, thanks - got some progress. The as: 'alias' worked and the odd TypeScript error is gone. Yes, it seems to not recognize annotations in home.js, though it does in app.ts. The only remaining TS error in app.ts is - can't find module 'home' in the import statement. However the Console shows a successful GET of home.js When you say configuration issue, do you mean file structure and relative paths that match? - or something else? app.ts and home.js are in the same directory and have the same import statement for @Component. What am I missing? – Mike_Laird Sep 02 '15 at 18:26
  • Here's a clue, but I don't know the answer. In app.ts when the import for Home is from "home", the TS error is can't find home. When import is from "/home", the TS error is - home has no exported member 'Home'. I've tried export class Home { } - it doesn't work. Would something else for the Home class statement work? – Mike_Laird Sep 02 '15 at 19:00
  • By configuration I mean system.js. See these plnkrs for example : [plnkr1](http://plnkr.co/edit/QzNONK?p=info) (using alpha36), [plnkr2](http://plnkr.co/edit/vRymOcLL5BiUdAtQhD5t?p=info) (using alpha36 with a little outdated systemjs and traceur). The errors you see now are in your IDE or when compiling the ts files? – Eric Martinez Sep 02 '15 at 19:02
  • Eric, I see in plnkr1 that cmpB is defined in app.ts. When I moved the component code in home.js into app.ts, all the TypeScript errors disappeared in my Atom editor (with atom-typescript). I thought import statements would bring the code in. It seems not. Is it true that components for routed partial pages all have to be in app.ts? Though the TS errors are fixed, I still don't have routing working. I have a test a link showing, but I don't have the 'wiring' right, I guess. The Console errors are No provider for Router and componentRef undefined. I'll try again. Thanks for plnkr1. – Mike_Laird Sep 03 '15 at 20:24
  • Several changes in alpha 36/35. I just updated my samples to reflect the latest changes: http://www.syntaxsuccess.com/viewarticle/routing-in-angular-2.0 – TGH Sep 03 '15 at 23:49
  • This approach will not work because it expects Angular2 to look through the and (magically) find tags to the next level partial page. Thrash and learn. – Mike_Laird Sep 14 '15 at 18:23
  • I didn't notice that `RouterLink` was missing from your `Home` component so I updated my answer. I also included sources where more detailed detailed info can be found. I'm currently building an angular2 website that also uses sub-components for header/footer/etc so I can verify that this should work. – Evan Plaice Nov 01 '15 at 06:31

2 Answers2

11

First, your route is broken

@RouteConfig( [  {path: '/home', as:  component: Home } ] )

You need to define the alias and make it CamelCase (technically PascalCase)

In addition the as parameter has been changed to name

@RouteConfig([{ path: '/home', name: 'Home', component: Home }])

Source:

Second, your router bootstrap is broken

bootstrap(ResidenceApp, [ routerInjectables ]);

routerInjectables has been replaced with ROUTER_PROVIDERS

bootstrap(ResidenceApp, [ ROUTER_PROVIDERS ]);

Source:

Third, you need to provide a correct path to Home

import { Home } from "home";

import angular2/angular2 works because config.js contains an alias for it. config.js does not contain aliases to your project components (unless you add them manually) so you have to import via the relative path.

import { Home } from "./components/home";

Fourth, your routerLink won't work

<a routerLink='home'>Home Page</a>`

It has to point to the alias, using the correct one-way binding.

<a [routerLink]="['./Home']">Home Page</a>`

Note: The link in the routerLink expression points to the alias (ie name field defined in the route so it also needs to be CamelCase.

Source:

Fifth, you need to configure the Home component to include RouterLink

import { RouterLink } from 'angular2/router';
...
@View({
  directives: [ RouterLink ],
  template: `<h1>Home page under construction</h1>
  <a routerLink='home'>Home Page</a>`
})
class Home {
...

Phew... I think that's everything.

Aside: Lots of breaking changes have been recently made to the router so basically any examples available online are broken.

Update:

Keep an eye out. In a future version RouterOutlet/<router-outlet> will be replaced by RouterViewport/<router-viewport>.

Update2:

The Route as parameter has been changed to name

Source:

Update2:

Breaking change:

[router-link] -> [routerLink]

Thanks to @Mike Laird's comment.

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
  • Evan, it took me a while to get back to this, but your comments got routing mostly working for me. Thanks. It routes forward to a new page, but not completely when going back to the home page. The home page URL appears in the browser's command bar, but the home page does not refresh automatically and appear. A click on the refresh button gets the home page to appear. Any thoughts on that? Thank you, very much. – Mike_Laird Nov 09 '15 at 19:13
  • Evan notes above that lots of breaking changes have occurred. Specifically, this Youtube, https://www.youtube.com/watch?t=10&v=ZsGRiHSaOxM has old broken syntax. It is well done and clear, but out of date. – Mike_Laird Nov 09 '15 at 19:15
  • When I click the link to a sub-page, I get the Console error - Component "PostApartment4Rent" has no route config. When I click the Back button, I get the Console exception - TypeError: instruction is undefined in [null] – Mike_Laird Nov 09 '15 at 19:37
  • @Mike_Laird I'm glad you were able to make some progress. Would you mind posting an update with the latest version of your code? It's kinda hard to infer the cause after all the changes. – Evan Plaice Nov 09 '15 at 21:51
  • 1
    Evan, we filled up this page, so I posted a new problem of not routing back at - http://stackoverflow.com/questions/33681486/angular2-routing-alpha-46-routes-forward-with-a-console-error-but-not-back-to Thank you, again. – Mike_Laird Nov 12 '15 at 21:20
  • Note that as of alpha.52, a breaking change afftects router-link. The 4th and 5th items above are now – Mike_Laird Dec 14 '15 at 20:15
  • Hi guys im just getting started with ng2 ... is this part of the tutorials updated : https://angular.io/docs/ts/latest/guide/router.html i'm getting this error : angular2/router has no exported member ROUTER_PROVIDERS reported by VSCode intellisense – Boban Stojanovski Feb 02 '16 at 19:23
  • @BobanStojanovski That should work. I'm using it here with `Angular2@2.0.0-beta.2` here https://github.com/evanplaice/evanplaice.com/blob/master/app/app.js. The API for that shouldn't change again with the official release to beta. – Evan Plaice Feb 02 '16 at 23:35
  • @Evan it's working for me also, but i was wondering why i got that error message although the routing worked fine. Anyway thanks for clarifying – Boban Stojanovski Feb 03 '16 at 07:54
  • @BobanStojanovski Looks like an Intellisense, not Angular2 bug. Haven't used VS in a few years so I don't have the slightest clue how to address it's quirks in the more recent versions. – Evan Plaice Feb 03 '16 at 19:53
0

After doing all the steps and still does not work, just try to set the base URL in the index.html using this:<base href="/"> after the <head> tag`.