【问题标题】:Angular2 router-deprecated dependencies not being loadedAngular2路由器不推荐使用的依赖项未加载
【发布时间】:2016-05-03 09:19:36
【问题描述】:

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

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

  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. 当我尝试加载该应用程序时,它会失败,因为它无法从已弃用的路由器解析所需的文件。我收到以下错误:

Image of error

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

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

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

    到你的packagesconfig.js

    或者如果你想使用新的路由器的话:

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

    来自rc.0 Plunker 的config.js 示例

    (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);
    

    【讨论】:

    • 您好,首先我添加了您的第一个解决方案,但它给我带来了新的错误。我相信无论如何还是尽快切换到新路由器会更好。我安装了新的路由器,但我不知道它是如何工作的。你能给我链接一个新路由器的教程/api吗?谢谢! :)
    • 我自己还没有找到。这里有几位 github.com/angular/angular/commit/602641d 这是我目前正在玩的 Plunker plnkr.co/edit/yRKhoOelf2uJ3yAsdIMm?p=info
    • 我查看了你的 plunker。似乎 @Router 装饰器破坏了我的 IDE 并且无法编译。我需要更新我的 ts 编译器吗?感谢大家的帮助!
    • 我明白了。那么 plunker sn-ps 工作,恭喜:)。我使用 angular2 基本站点提供的 Microsoft 代码和标准 tsconfig.json、typings.json。嗯,这可能需要一些时间才能更新文档或其他内容。
    • 我现在一切正常。我的 ide 有一些问题,并且我犯了一些错误。再次感谢您!
    【解决方案2】:

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

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

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

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

    然后,在使用它之前不要忘记在你的项目中运行npm i来安装这个模块。

    【讨论】:

      【解决方案3】:

      其实解决方法很简单, 我们需要更改 systemjs.config.js 文件。 packages 数组包含 Angular 包的列表,而其中一个包是错误的

      替换这个:'@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',
                ];
      

      就是这样。

      【讨论】:

        猜你喜欢
        • 2016-10-21
        • 2017-01-01
        • 2021-05-29
        • 2023-03-14
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 2016-08-17
        • 1970-01-01
        相关资源
        最近更新 更多