我的index.js
const test = require('./js/lerifier/start');
const $ = require("jquery");
$( document ).ready(function() {
console.log( "ready!" );
console.log(test.init());
});
我想从this.init()
start.js
函数
const $ = require("jquery");
const SVGInjector = require('svg-injector');
module.exports = function Verifier() {
this.init = function () {
let mySVGsToInject = $('img.inject-me');
let injectorOptions = {
evalScripts: 'once',
pngFallback: 'assets/png',
each: function (svg) {
// Callback after each SVG is injected
console.log('SVG injected: ' + svg.getAttribute('id'));
}
};
// Trigger the injection
SVGInjector(mySVGsToInject, injectorOptions, function (totalSVGsInjected) {
// Callback after all SVGs are injected
console.log('We injected ' + totalSVGsInjected + ' SVG(s)!');
});
}
};
使用index.js
中所述的代码,我得到的错误消息是
test.init is not a function
,我知道我做错了什么或误解了module.exports的使用。
答案 0 :(得分:-1)
这样的事情怎么样?在这种情况下,直接引用start.js
中的函数 const test = require('./js/lerifier/start.js').Verifier;