Angular 7 js2xmlparser TypeError:处理程序不是函数

问题描述 投票:0回答:1

我正在使用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));
javascript angularjs angular xml-parsing
1个回答
0
投票

您需要:

  1. 安装https://www.npmjs.com/package/js2xmlparser

  2. 安装https://www.npmjs.com/package/@types/node

  3. [从* js2xmlparser导入*作为js2xmlparser;]]

  4. 将js2xmlparser的引用添加到angular.json

  5. 中的脚本中
"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"
            ]
          }
  1. 将代码更改为:
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

© www.soinside.com 2019 - 2024. All rights reserved.