我正在使用js2xmlparser将对象转换为xml。一切在Angular8中都可以正常工作,但是当我在Angular7中使用相同的代码时,会引发错误“ TypeError:handler不是函数”。在下面提供了示例代码。
let js2xml = require('js2xmlparser');
let obj = {
"firstName": "John",
"lastName": "Smith",
"dateOfBirth": new Date(1964, 7, 26),
"address": {
"@": {
"type": "home"
},
"streetAddress": "3212 22nd St",
"city": "Chicago",
"state": "Illinois",
"zip": 10000
}
};
console.log(js2xml.parse("person", obj));
您需要:
[从* js2xmlparser导入*作为js2xmlparser;]]
- 中的脚本中
将js2xmlparser的引用添加到angular.json
"architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets", { "glob": "**/*", "output": "./assets/" } ], "styles": [], "scripts": [ "node_modules/js2xmlparser/dist/js2xmlparser.min.js" ] }
- 将代码更改为:
let obj = { "firstName": "John", "lastName": "Smith", "dateOfBirth": new Date(1964, 7, 26), "address": { "@": { "type": "home" }, "streetAddress": "3212 22nd St", "city": "Chicago", "state": "Illinois", "zip": 10000 } }; console.log(js2xmlparser.parse("person", obj));
输出:
<?xml version='1.0'?> <person> <firstName>John</firstName> <lastName>Smith</lastName> <dateOfBirth>Wed Aug 26 1964 00:00:00 GMT+1000 (Australian Eastern Standard Time)</dateOfBirth> <address type='home'> <streetAddress>3212 22nd St</streetAddress> <city>Chicago</city> <state>Illinois</state> <zip>10000</zip> </address> </person>
工作示例:
https://stackblitz.com/edit/angular-nf8k31-3nb6jh?file=app%2Fmodal-component.module.ts