【问题标题】:Angular 2 - Karma cannot find the external template or style fileAngular 2 - Karma 找不到外部模板或样式文件
【发布时间】:2017-07-21 14:36:43
【问题描述】:

文件结构(Angular 2官网的快速入门项目):

一开始,在启动 Karma 时,我收到错误说在 /@angular/ 下找不到两个文件...

我发现我必须更改 systemjs.config.js 中的路径才能使其正常工作:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': '/base/node_modules/'  <<<<<<<------ Vincent: to be able to run the test, 
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

所以,至少 Karma 现在可以工作了,我可以运行其他测试。但是,组件测试失败,因为它找不到组件的外部模板和样式文件。

我曾经可以通过在我的 Chrome 中打开:http://localhost:9876/base/src/app/app.component.html 来打开 html,但现在找不到了。

尝试了这个答案:https://stackoverflow.com/a/39909929/3634727,所有解决方案都在这里:Error: Uncaught (in promise): Failed to load login.component.html,没有运气。

我的第 1 个问题是,让我们把设置放在一边,在我的浏览器中打开 app.component.html 的正确 URL 是什么? http://localhost:9876/src/app/app.component.html 不起作用。

【问题讨论】:

    标签: javascript angular unit-testing


    【解决方案1】:

    我解决了它并想分享我的发现。

    首先,回答我自己的第一个问题,URI (http://localhost:9876/base/src/app/app.component.html) 是正确的,Karma 通过这个 URI 解决文件。重新启动我的电脑后,我可以通过直接在我的 Chrome 中打开它来访问它。

    Karma 提供的根路径与运行应用程序的根路径不同,因此,一个好的做法是在组件的注释中使用外部模板和样式文件的相对路径,例如:

    // imports...
    
    @Component({
        selector: 'my-app',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css'],
        providers: [RuntimeSettingsService]
    })
    export class AppComponent implements OnInit {
        //...
    }
    

    在这两种情况下它都适用于根路径的设置。更多详情请参考此博客:Run Unit Tests On Angular 2 Quick-Start Example With Jasmine & Karma

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 2016-07-17
      相关资源
      最近更新 更多