Angular2路由器弃用的依赖项未加载

时间:2016-05-03 09:19:37

标签: angular angular2-routing

我最近更新了我的angular2版本并遇到了以下问题:

  1. 路由器lib不再存在,已被路由器弃用。

  2. 我有这个菜单组件:

    import { Component }       from '@angular/core';
    import { DashboardComponent } from './dashboard.component'
    import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';
    
    @Component({
      selector: 'main-menu',
      templateUrl: 'app/views/menu.html',
      directives: [ROUTER_DIRECTIVES],
      providers: [
        ROUTER_PROVIDERS
      ]
    })
    
    @RouteConfig([
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: DashboardComponent,
        useAsDefault: true
      }
    ])
    
    export class MenuComponent {}
    
  3. 当我尝试加载它时,应用程序失败,因为它无法解析路由器所需的文件 - 已弃用。我收到以下错误:

  4. Image of error

3 个答案:

答案 0 :(得分:4)

在Angular2 RC.0中,您可能需要添加

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

packages

中的config.js

或者如果您想使用新路由器:

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

config.js Plunker

中的rc.0示例
(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);

答案 1 :(得分:4)

更准确地说,使用typescript项目,在package.json文件中添加模块依赖项:

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

在您的src/system-config.ts文件中这一行:

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

然后,不要忘记在项目中运行npm i以便在使用之前安装此模块。

答案 2 :(得分:3)

实际上解决方案非常简单, 我们需要更改systemjs.config.js文件。 packages数组包含有角度包的列表,而其中一个是错误的

替换它:' @ angular / router', 用这个:' @ 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',
          ];

是的。