Angular2 router-deprecated dependencies not being loaded

拈花ヽ惹草 提交于 2019-11-27 05:28:58

In Angular2 RC.0 you might need to add

'@angular/router-deprecated': {
  main: 'index.js',
  defaultExtension: 'js'
},

to your packages in config.js

or this if you want to use the new router:

'@angular/router': {
  main: 'index.js',
  defaultExtension: 'js'
},

Example config.js from the rc.0 Plunker

(function(global) {

  var ngVer = '@2.0.0-rc.0'; // lock in the angular package version; do not let it float to current!

  //map tells the System loader where to look for things
  var  map = {
    'app':                        'src', // 'dist',
    'rxjs':                       'https://npmcdn.com/rxjs@5.0.0-beta.6',
    'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api' // get latest
  };

  //packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'app.ts',  defaultExtension: 'ts' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { defaultExtension: 'js' },
  };

  var packageNames = [
      '@angular/common',
      '@angular/compiler',
      '@angular/core',
      '@angular/http',
      '@angular/platform-browser',
      '@angular/platform-browser-dynamic',
      '@angular/router-deprecated',
      '@angular/testing',
      '@angular/upgrade',
  ];

  // add map entries for angular packages in the form '@angular/common': 'https://npmcdn.com/@angular/common@0.0.0-3'
  packageNames.forEach(function(pkgName) {
    map[pkgName] = 'https://npmcdn.com/' + pkgName + ngVer;
  });

  // add package entries for angular packages in the form '@angular/common': { main: 'index.js', defaultExtension: 'js' }
  packageNames.forEach(function(pkgName) {
    packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
  });

  var config = {
    transpiler: 'typescript',
    typescriptOptions: {
      emitDecoratorMetadata: true
    },
    map: map,
    packages: packages
  }

  // filterSystemConfig - index.html's chance to modify config before we register it.
  if (global.filterSystemConfig) { global.filterSystemConfig(config); }

  System.config(config);

})(this);

More exactly, with typescript project, add the module dependency in the package.json file:

dependencies{
    ...
   "@angular/router-deprecated": "2.0.0-rc.1"
}

And in your src/system-config.ts file this line:

const barrels:string[] = [
     ...
    '@angular/router-deprecated'
];

Then, don't forget to run npm i in your project to install this module before to use it.

Actually the solution is very simple, we need to change the systemjs.config.js file. the packages array holds list of angular packages while one of them is wrong

replace this: '@angular/router', with this: '@angular/router-deprecated',

    systemjs.config.js:
...
...
...

        var packageNames = [
            '@angular/common',
            '@angular/compiler',
            '@angular/core',
            '@angular/http',
            '@angular/platform-browser',
            '@angular/platform-browser-dynamic',
            '@angular/router-deprecated',
            '@angular/testing',
            '@angular/upgrade',
          ];

that's it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!