业力-包含html文件时出现意外令牌 [英] Karma - Unexpected token when including an html file

查看:87
本文介绍了业力-包含html文件时出现意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的业力配置文件中包含一个简单的html文件,以访问我的javascript文件中的html元素,并使用业力中的茉莉花对此进行测试.

I'm trying to include an simple html file in my karma config file to access the html elements from my javascript files and test this with jasmine in karma.

但是我总是得到一个意外令牌< 错误.我在网络广播中看到可以包含h​​tml文件,但是我不知道为什么它不能与我的配置一起使用?

But I'm getting always an Unexpected token < error. I saw in an webcast that it is possible to include html files, but I don't know why it doesn't works with my configuration?

预先感谢

推荐答案

您需要使用 html2js预处理器(或 ng-html2js 预处理器AngularJS).有关预处理器的工作原理,请参见.

You need use the html2js preprocessor (or the ng-html2js preprocessor if using AngularJS). See the preprocessor description for how it works.

从Karma 0.10开始,html2js预处理器随Karma一起提供并默认配置:

Since Karma 0.10, the html2js preprocessor is shipped with Karma and configured by default:

module.exports = function(config) {
  config.set({
    files: [
      'templates/*.html'
    ],
    preprocessors: {
      '**/*.html': ['html2js']
    }
  });
};

然后您可以以JS字符串的形式访问html内容:

Then you can access the html content as JS string:

var elm = $(__html__['templates/x.html'])

您收到的错误表示HTML文件未经过预处理.默认情况下,basePath下的所有HTML文件都是经过预处理的,因此您的HTML文件可能位于其他位置,在这种情况下,您需要将它们明确地放在preprocessors配置中.如果您需要更多帮助,请共享您的karma.conf.js.

The error you are getting means that the HTML files are not preprocessed. By default, all HTML files under the basePath are preprocessed, so maybe your HTML files are somewhere else, in which case you need to put them in the preprocessors config explicitly. Share your karma.conf.js if you need more help.

这篇关于业力-包含html文件时出现意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆