我是 Angular 和 Nodejs 的新手。我目前正在使用Angular 5开发Web应用程序。我想创建一个节点模块并将其发布到 npm 并在我的应用程序中使用。该模块应从 xml 文件读取以创建一些对象。它总是给我同样的错误(ref Foo<>
)。我不知道是否可以从节点模块内的 xml 中读取。
我正在尝试创建一个npm包,在该包中将有一个xml文件。在此包的类之一中,我想读取xml文件。
Here is screenshot from @types/node module. The function exists
这是我的fs_1.readFile is not a function
:
index.js
这是索引。d.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const xml_js_1= require("xml-js");
class PasswordsConfiguration{
constructor(){
this.passwords=new Array();
this.companies=new Array();
this._json = undefined;
this.readXML()
}
readXML(){
var name,isFunction, maskAND, maskOR, key, defaultValue,companyName,displayedName;
try{
fs_1.readFile('./passwords-config.xml', 'utf8', (err, data) => {
// just neglect the rest of the code, it doesn#t enter this function.
if (err) {
}
else {
this._json = xml_js_1.xml2js(data, {
compact: true,
nativeType: true,
ignoreDeclaration: true,
ignoreComment: true
});
this._json.Configuration.Passwords.every((password) => {
name =password._attributes.name;
isFunction=Boolean(password._attributes.isFunction);
maskAND = parseInt(password._attributes.maskAND,16);
maskOR = parseInt(password._attributes.maskOR,16);
key = password._attributes.key;
defaultValue = parseInt(password._attributes.defaultValue,16);
this.passwords.push(new Password(name,isFunction, maskAND, maskOR, key, defaultValue));
});
this._json.Configuration.Companies.every((company) => {
companyName =company._attributes.name;
displayedName =company._attributes.displayedName;
this.companies.push(new Company(companyName,displayedName));
});
}
});
}catch(e){
}
}
}
exports.PasswordsConfiguration = PasswordsConfiguration;
此功能适用吗?代码有什么问题?