我对此有点困惑。我有一个由 middleman、karma、jasmine、babeljs 组成的复杂堆栈来构建静态网站。
考虑到这是一个实验,我想使用带有模块的 ES6。不过,中间人方面一切都很好,我在设置 karma + jasmine 进行测试时遇到了困难。
主要问题在于 babel 中:如果您将其设置为使用,modules: "ignore"则必须手动使用规范中的所有System.import模块,这是我不想要的。我想使用 ES6 语法,但如果我设置,babeljs 会将我的所有测试包装到 中,如下所示:modules: "system"System.register
System.register(["mymodule"], function (_export) {
"use strict";
var Mymodule;
return {
setters: [function (_mymodule) {
Mymodule = _mymodule["default"];
}],
execute: function () {
console.log("I'm here!!!");
console.log(Mymodule);
describe("Mymodule", function () {
it("has version", function () {
expect(Mymodule.VERSION).toEqual("1.0.0");
});
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
因此测试不会自动执行。然后,我创建了以下脚本来解决这个问题(在包含所有规格后包含该脚本):
basePath = "/base/spec/"
modules = []
for own fileName, fileHash of window.__karma__.files
if fileName.indexOf(basePath) …Run Code Online (Sandbox Code Playgroud) 我正在使用 Mocha 和 TypeScript 编写测试......这是一个简单的示例:
import {assert} from 'chai';
import Greeting from '../../../src/component/greeting/greeting';
describe('Greeting component', function () {
it('should greet correctly', function () {
let greeting = new Greeting();
assert(greeting.greeting === 'Hello World', 'should express the correct greeting');
});
});
Run Code Online (Sandbox Code Playgroud)
我可以看到这些被正确编译。我将它们作为 common-js 模块输出,并使用 system-js 作为浏览器内加载器。
System.import('component/greeting/greetingSpec.js')
.catch(console.log.bind(console))
.then(function() {
mocha.run();
})
Run Code Online (Sandbox Code Playgroud)
我想创建一个列出所有“spec”文件的文件:
import * as greeting from './greeting/greetingSpec';
import * as foo from './greeting/fooSpec';
Run Code Online (Sandbox Code Playgroud)
然而,TypeScript 编译器可以看到这些导入未被使用,因此不会将它们包含在 JS 输出中。
如何为我的测试定义一个可以通过 system-js 加载的“入口点”?
我正在尝试将节点模块导入 Angular 2 但无济于事。
如何才能实现这一目标?
我正在尝试将以下模块导入 Angular 2: https://www.npmjs.com/package/countryjs
我是否需要使用 system.js 将其注册到当前应用程序中:
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
},
map: {
'countryjs': 'node_modules/countryjs/lib/countryjs'
},
meta: {
// sets meta for modules within the package
'vendor/*': {
'format': 'global'
}
}
}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>
Run Code Online (Sandbox Code Playgroud)
然后在 ts 文件中,我使用:
import {} from "countryjs"
Run Code Online (Sandbox Code Playgroud)
当我使用这个(如下)时,它给了我一个错误
import {Country} from "countryjs"
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我收到的错误是:异常:ReferenceError:[null]中未定义强制转换
编写自定义 Chromecast 接收器应用程序需要使用以下 js 文件,该文件通过全局“ cast ”变量公开功能:
//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js
我想使用 systemjs 将此脚本作为模块加载,而不是在 index.html 中使用脚本标记。
这是一个 Angular2 和 TypeScript 应用程序,因此在某些服务 MyService 中,我希望能够导入“cast”并使用它。
索引.html
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
},
paths: {
'cast': '//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js'
},
meta: {
'//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js': {format: 'global'}
},
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
Run Code Online (Sandbox Code Playgroud)
myService.ts
import {Injectable} from 'angular2/core';
import cast from 'cast';
@Injectable()
export class MyService {
public init = () => {
cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.NONE);
}
...
}
Run Code Online (Sandbox Code Playgroud)
可以从以下位置克隆示例 Angular2 + TypeScript + Chromecast …
我正在构建一个 Angular 2 应用程序并使用 Chart.js 2.0 来显示“时间”数据。不幸的是,我无法更改 moment.js 的语言环境。
我 npm 安装了 moment 并使用 SystemJS 导入了 moment.js 和 pl.js,但是 Chart.js 抱怨必须在 Chart.js 之前加载 moment.js(不确定如何实现,在每个源文件中,moment 都在 chartjs 之前声明)。
然后我尝试了 Chart.bundle.min.js(包括 moment),它可以工作,但只能用英语。我无法将语言环境更改为“pl”。
以下行对 Chart.bundle.min.js 没有影响,日期仍以英文显示。
moment.locale('pl');
Run Code Online (Sandbox Code Playgroud)
Chart.js 是否有自己的功能来更改语言环境?文档没有指定关于帽子的任何内容。
我正在尝试使用 Karma 和 Jasmine 在 phantomJS 中运行我的单元测试。单元测试和源文件都是用 Typescript 编写的。 测试在 Chrome、Safari 和 Firefox 中运行良好,但在 PhantomJS 中失败。错误:
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
Error: (SystemJS) eval@[native code]
tryCatchReject@/poc/node_modules/systemjs/dist/system-polyfills.src.js:1188:34
runContinuation1@/poc/node_modules/systemjs/dist/system-polyfills.src.js:1147:18
when@/poc/node_modules/systemjs/dist/system-polyfills.src.js:935:20
run@/poc/node_modules/systemjs/dist/system-polyfills.src.js:826:17
_drain@/poc/node_modules/systemjs/dist/system-polyfills.src.js:102:22
drain@/poc/node_modules/systemjs/dist/system-polyfills.src.js:67:15
Evaluating /poc/src/components/button/button.js
Error loading /poc/test/components/button/button.spec.ts
Run Code Online (Sandbox Code Playgroud)
我试图连接 phantomjs 调试器,但它没有抛出任何错误,所以我有点卡在这里。
业力配置:
module.exports = function (config) {
config.set({
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
}
},
flags: ['--load-images=true'],
debug: true
}
},
// base path that will be used to …Run Code Online (Sandbox Code Playgroud) 如何使用 SystemJS 和使用这个 system.config.js 文件将我刚刚从 npm 下载的新包添加到我的 Angular 2 组件中。下面的代码是由一个入门包为我生成的。我试图在这个文件的地图和包部分放置模块的链接,但它似乎不起作用。我只想知道如何获取位于 node_modules 中的下划线 js 等库并将其输入到此文件中,以便我可以轻松地将文件导入到我的打字稿角度组件中。
var isPublic = typeof window != "undefined";
(function(global) {
var map = {
'app': 'client', // 'dist',
'@angular': (isPublic)? '@angular' : 'node_modules/@angular',
'@angular/router': (isPublic)? '@angular/router' : 'node_modules/@angular/router',
'angular2-in-memory-web-api': (isPublic)? 'angular2-in-memory-web-api' : 'node_modules/angular2-in-memory-web-api',
'rxjs': (isPublic)? 'rxjs' : 'node_modules/rxjs',
'ng-semantic': (isPublic)? 'ng-semantic' : 'node_modules/ng-semantic'
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.js', …Run Code Online (Sandbox Code Playgroud) 我有这个类,我尝试使用 new 关键字实例化Ajv,但出现此错误:
类型错误:Ajv 不是构造函数
代码:
import * as Ajv from "ajv";
export class ValidateJsonService {
validateJson(json, schema) {
console.log(Ajv);
let ajv = new Ajv({ allErrors: true });
if (!ajv.validate(schema, json)) {
throw new Error("JSON does not conform to schema: " + ajv.errorsText())
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制台日志:
这段代码曾经有效,这就是 Ajv 的使用方式。从 Ajv 文档:
最快的验证调用:
var Ajv = require('ajv');
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
var validate = ajv.compile(schema);
var valid = validate(data); …Run Code Online (Sandbox Code Playgroud) 当我使用 systemjs 和 ASP.NET MVC4 安装 angular4 时,我遇到了这个错误。我试图通过注释代码来解决这个问题
"throw new error('Angular requires Zone.js prolyfill.')"
Run Code Online (Sandbox Code Playgroud)
在 node_modules/@angular/core/core.umd.js. 但没有任何结果。
我的确切问题是。
未捕获(承诺)错误:Angular 需要 Zone.js prolyfill。在新的 NgZone (core.umd.js:3805) at PlatformRef_._bootstrapModuleFactoryWithZone (core.umd.js:4524) at eval (core.umd.js:4569) at
如果有人知道如何解决此问题,请帮助我。
我在我的 cshtml 文件中使用的依赖项
<script src="~/node_modules/core-js/client/shim.min.js"></script>
<script src="~/node_modules/zone.js@0.8.4/dist/zone.js"></script>
<script src="~/node_modules/reflect-metadata/Reflect.js"></script>
<script src="~/node_modules/systemjs/dist/system.src.js"></script>
<script src="~/systemjs.config.js"></script>
<script>
System.import('app').catch(function (err)
{
console.error(err);
});
</script>
Run Code Online (Sandbox Code Playgroud) 我正在使用 systemjs 创建一个 angular 7 项目来动态加载模块。
当我尝试使用它时,出现以下错误:
ERROR ReferenceError: SystemJS is not defined
Run Code Online (Sandbox Code Playgroud)
我的 package.json 包含 systemjs: 2.1.1
我在 angular.json 中将 systemjs 路径添加到了部分脚本
"./node_modules/systemjs/dist/system.js"
Run Code Online (Sandbox Code Playgroud)
我声明 SystemJs 在我的服务中使用它:
declare const SystemJS;
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样使用,在这个函数中:
loadModuleSystemJS(moduleInfo: ModuleData): Promise<any> {
const url = this.source + moduleInfo.location;
SystemJS.set('@angular/core', SystemJS.newModule(AngularCore));
SystemJS.set('@angular/common', SystemJS.newModule(AngularCommon));
SystemJS.set('@angular/router', SystemJS.newModule(AngularRouter));
SystemJS.set('@angular/platform-browser/animations', SystemJS.newModule(BrowserAnimations));
// now, import the new module
return SystemJS.import(`${url}`).then((module) => {
return this.compiler.compileModuleAndAllComponentsAsync(module[`${moduleInfo.moduleName}`]).then(compiled => {
return module;
});
});
}
Run Code Online (Sandbox Code Playgroud)
也许我错过了什么,
你能帮助我吗 ?
systemjs ×10
angular ×7
javascript ×6
typescript ×5
ajv ×1
angular7 ×1
angularjs ×1
babeljs ×1
chart.js ×1
chromecast ×1
jasmine ×1
karma-runner ×1
momentjs ×1
node-modules ×1
npm ×1
phantomjs ×1