本文介绍了Angular2路由器弃用的依赖项未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have recently updated my angular2 version and have had the following issue:

  1. 路由器库不再存在,它已被不推荐使用的路由器所取代.

  1. The router lib no longer exists, it has been replaced by router-deprecated.

我有这个菜单组件:

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 {}

  • 当我尝试甚至加载该应用程序时,该应用程序失败,因为它无法解析不赞成使用路由器的文件.我收到以下错误:

  • The app fails when I try to even load it, as it is unable to resolve the files needed from the router-deprecated. I get the following error:

    错误图片

    推荐答案

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

    In Angular2 RC.0 you might need to add

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

    config.js

    或者如果要使用新路由器,则执行以下操作:

    or this if you want to use the new router:

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

    rc.0柱塞中的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);
    

    这篇关于Angular2路由器弃用的依赖项未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-24 18:48
    查看更多