[英]wallaby.js with jQuery
我們在帶有Typescript的Angular 4項目中使用wallaby.js,其中有些部分使用jQuery。 Wallaby.js引發ReferenceError:
ReferenceError: $ is not defined
任何想法如何解決這一問題?
一些示例代碼:
import { Injectable } from '@angular/core';
declare var $: any;
@Injectable({
providedIn: 'root'
})
export class FroalaService {
constructor() { }
public initFroala(): void {
$.FroalaEditor.DefineIcon('insertnonbreakingspace', {NAME: 'arrows-h'});
$.FroalaEditor.RegisterCommand('insertnonbreakingspace', {
title: 'Non-Breaking Space',
focus: true,
undo: true,
refreshAfterCallback: true,
callback: function () {
this.html.insert('<span style="white-space: nowrap;"> </span>');
}
});
$.FroalaEditor.RegisterShortcut(32, 'insertnonbreakingspace', null, 'Space', false, false);
}
}
基本jQuery實現的解決方案是在wallaby.js中向外部添加jQuery:
var webpackPostprocessor = wallabyWebpack({
entryPatterns: [
'src/wallabyTest.js',
'src/**/*spec.js'
],
module: {
rules: [
...
]
},
resolve: {
extensions: ['.js', '.ts'],
modules: [
...
]
},
node: {
...
},
externals: { jQuery: "jQuery" }
});
...並將jquery.js添加到文件中:
return {
files: [
{pattern: "node_modules/jquery/dist/jquery.js", instrument: false},
{pattern: 'src/**/*.+(ts|css|less|scss|sass|styl|html|json|svg)', load: false},
{pattern: 'src/**/*.d.ts', ignore: true},
{pattern: 'src/**/*spec.ts', ignore: true}
],
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.