【问题标题】:How to use node.js module with Angular 6?如何在 Angular 6 中使用 node.js 模块?
【发布时间】:2018-08-06 17:37:06
【问题描述】:

我是 Angular 6 和 TypeScript 的新手。有人可以告诉我在 Angular 6 中使用 node.js 第三方模块的正确方法吗?

例如,我想创建能够消费和向 SOAP wsdl 方法发出请求的组件。

是否通过使用 npm install 添加到 package.json 来安装它。

尝试像这样使用 node-soap npm 模块:

import { Injectable } from '@angular/core';
import * as soap from 'node-soap';

@Injectable({
  providedIn: 'root'
})
export class MySoapService {
  constructor() { }

  getOrderInfo() {
    const url = 'http://my-example-api.com/WCF/ClientService.svc?singleWsdl';
    let args = {
      login: 'login',
      password: 'password',
      orderNumber: 'F976638'
    };
    soap.createClient(url, function(err, client) {
      client.GetOrderInfo(args, function(err, result) {
        console.log(result);
      });
    });
  };
}

然后将这个服务注入到组件中并渲染,只是为了测试服务...

但在ng serve 期间出现了一些错误:

ERROR in ./node_modules/node-soap/client.js
Module not found: Error: Can't resolve 'http' in '/home/cadistortion/WebstormProjects/my-ng-app/node_modules/node-soap'

谢谢!

【问题讨论】:

  • 节点库旨在在服务器上的 NodeJS 环境中执行。 Angular 是一个客户端框架:Angular 应用程序在浏览器中执行。您不能只使用任何节点库并在 Angular 应用程序中使用它。
  • @JBNizet - 似乎是一个公平的问题。理论上,它们都是Javascript。为什么不能在客户端重用一些更简单的节点功能(不依赖服务器)?在这里找到了一些想法:geeksforgeeks.org/…

标签: node.js angular angular6


【解决方案1】:

这很简单,在您的 url 中有您的 API 节点。 你需要更多,express.js(或其他)来工作。 BD - 像 MongoDB 或其他或 Json。

看到这个视频和其他相同的用户https://www.youtube.com/watch?v=M-G48Gf2Xl0 完成了如何使用 MEAN Stack 创建博客

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 2023-03-13
    • 2020-01-15
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多