简体   繁体   English

Angular 2-Karma无法找到外部模板或样式文件

[英]Angular 2 - Karma cannot find the external template or style file

File structure (it is the quickstarter project from Angular 2 official site ): 文件结构(这是来自Angular 2官方网站的quickstarter项目):

在此处输入图片说明

In the very beginning, when starting Karma, I got errors saying cannot find two files under /@angular/... 刚开始时,启动Karma时出现错误,提示无法在/ @ angular /下找到两个文件。

I found I had to change the path in systemjs.config.js, to make it work: 我发现我必须更改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);

So, at least Karma works now, I can run other tests. 因此,至少Karma现在可以运行,我可以运行其他测试。 However, the component tests fail, because it cannot find the external template and style files of the component. 但是,组件测试失败,因为它找不到组件的外部模板和样式文件。

在此处输入图片说明

I once was able to open the html by opening up: http://localhost:9876/base/src/app/app.component.html in my Chrome, and cannot find it anymore. 我曾经能够通过在Chrome中打开http:// localhost:9876 / base / src / app / app.component.html来打开html,但现在找不到了。

Tried this answer: https://stackoverflow.com/a/39909929/3634727 , and all the solutions in here: Error: Uncaught (in promise): Failed to load login.component.html , no luck. 尝试了以下答案: https : //stackoverflow.com/a/39909929/3634727 ,以及此处的所有解决方案: 错误:未被发现(承诺):无法加载login.component.html ,没有运气。

My number 1 question is, let's put the settings aside, what is the correct URL to open the app.component.html in my browser? 我的第一个问题是,让我们搁置设置,在浏览器中打开app.component.html的正确URL是什么? http://localhost:9876/src/app/app.component.html does not work. http:// localhost:9876 / src / app / app.component.html不起作用。

I solved it and would like to share what I found. 我已解决它,并希望分享我发现的内容。

First thing first, to answer my own No.1 question, the URI ( http://localhost:9876/base/src/app/app.component.html ) is correct, Karma solves the file through this URI. 首先,要回答我自己的第一个问题,URI( http:// localhost:9876 / base / src / app / app.component.html )是正确的,Karma通过此URI解决了文件。 After re-booting my PC, I can access it by opening it directly in my Chrome. 重新启动PC后,可以直接在Chrome中打开它来访问它。

The root path which Karma serves is different from that root path for running the app, so, a good practice is to use relative path for external template & style files in the component's annotation, like: Karma提供的根路径与用于运行应用程序的根路径不同,因此,一个好的做法是在组件的注释中为外部模板和样式文件使用相对路径,例如:

// imports...

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

It works with the settings of root path in both cases. 在两种情况下都可以使用根路径的设置。 For more details, please refer to this blog: Run Unit Tests On Angular 2 Quick-Start Example With Jasmine & Karma . 有关更多详细信息,请参阅此博客: 使用Jasmine&Karma在Angular 2快速入门示例上运行单元测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM