slipperypete
slipperypete

Reputation: 6246

Angular2 not loading routes file

I am trying to switch my angular2 app from the deprecated router to the recommended router [https://angular.io/docs/ts/latest/guide/router.html][1].I am building my project with help from the angular2 cli. Ive successfully done this with a seed project without the the cli, but when doing this with the cli, I keep getting this error:

Error loading http://localhost:4200/app.routes as "./app.routes" from http://localhost:4200/main.js" ; Zone: ; Task: Promise.then ; Value: Error: patchProperty/desc.set/wrapFn@http://localhost:4200/vendor/zone.js/dist/zone.js:769:27 Zonehttp://localhost:4200/vendor/zone.js/dist/zone.js:356:24 Zonehttp://localhost:4200/vendor/zone.js/dist/zone.js:256:29 ZoneTask/this.invoke@http://localhost:4200/vendor/zone.js/dist/zone.js:423:29 Error loading http://localhost:4200/app.routes as "./app.routes" from http://localhost:4200/main.js

This is my main.ts file.

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment } from './app/';
import { APP_ROUTER_PROVIDERS } from './app.routes';

if (environment.production) {
  enableProdMode();
}

bootstrap(AppComponent, [APP_ROUTER_PROVIDERS])
  .catch(err => console.error(err));

This is my app.routes.ts file

import { provideRouter, RouterConfig } from '@angular/router';
import {Main} from "./app/splash_app/main.component";
import {Whatsup} from "./app/splash_app/whatsup.component";
import {LocalBus} from "./app/splash_app/localbus.component";
import {Login} from "./app/splash_app/login.component";

export const routes: RouterConfig = [
  { path: 'main', component: Main },
  { path: 'search', component: Whatsup },
  { path: 'local-business', component: LocalBus },
  {path: 'guest-login', component: Login}
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

This is the system-config file

// SystemJS configuration file, see links for more information
// https://github.com/systemjs/systemjs
// https://github.com/systemjs/systemjs/blob/master/docs/config-api.md

/***********************************************************************************************
 * User Configuration.
 **********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
};

/** User packages configuration. */
const packages: any = {

};

////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
 * Everything underneath this line is managed by the CLI.
 **********************************************************************************************/
const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',


  // Thirdparty barrels.
  'rxjs',

  // App specific barrels.
  'app',
  'app/shared',
  /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
  cliSystemConfigPackages[barrelName] = { main: 'index' };
});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
  map: {
    '@angular': 'vendor/@angular',
    'rxjs': 'vendor/rxjs',
    'main': 'main.js'
  },
  packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

Both the main.ts file and app.routes.ts file are in the src folder.

Upvotes: 0

Views: 1987

Answers (1)

Brocco
Brocco

Reputation: 64863

Move the app.routes.ts file into your app directory and then change the import within main.ts to point at the new path....

import { APP_ROUTER_PROVIDERS } from './app/app.routes';

Upvotes: 0

Related Questions