Angular TypeScript在声明函数时显示错误

Angular TypeScript在声明函数时显示错误,angular,typescript,Angular,Typescript,在执行我的代码时,typescript错误显示“require”不是函数,这就是为什么我事先声明了函数,但现在typescript抱怨“修饰符不能出现在这里”。有人能帮我吗 import { Injectable, ValueProvider } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class DataService { edge : any; constructor() { d

在执行我的代码时,typescript错误显示“require”不是函数,这就是为什么我事先声明了函数,但现在typescript抱怨“修饰符不能出现在这里”。有人能帮我吗

import { Injectable, ValueProvider } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  edge : any;

  constructor() {
    declare function require(name: string); // "Modifiers cannot appear here."
    this.edge = require('edge');
  }

  getData(type: string, method: string) {
    var data = this.edge.func({
      assemblyFile: 'C:\Program Files\Common Files\Siemens\Automation\Simatic OAM\bin\CC.CommonInterfaces.dll',
      typeName: `CC.CommonInterfaces.${type}`,
      methodName: `${method}`
    });

    return data(null, function(error, result) {
      if (error) throw error;
      console.log(result);
      return result;
    });
  }

}
您可以直接导入边缘,而不必使用require:


你能提供一个简单的工作示例吗?也许是闪电战?还有,你到底想做什么?edge是您要导入的依赖项吗?@Wernerson不知道如何显示这方面的最小工作示例。但我可以解释我在这里做什么。我基本上是从DataService中的.dll调用C#函数,并希望在我的Angular项目中使用数据(来自getData())。要调用C#函数,我需要edge.js,它需要一个require()函数来调用,typescript直到运行时才知道这个函数,这就是为什么我事先声明了这个函数,但它不起作用。为什么你还要使用require?你应该导入。。。否则,请在
.d.ts
文件中使用
声明
。在我看来,edge是一个依赖项,因此,需要像导入
可注入
值提供程序
表单
@angular/core
一样导入。请回答我的问题。
import { Injectable, ValueProvider } from "@angular/core";

import * as edge from "edge";

@Injectable({
  providedIn: "root"
})
export class DataService {
  constructor() {}

  getData(type: string, method: string) {
    let data = edge.func({
      assemblyFile:
        "C:Program FilesCommon FilesSiemensAutomationSimatic OAM\binCC.CommonInterfaces.dll",
      typeName: `CC.CommonInterfaces.${type}`,
      methodName: `${method}`
    });

    return data(null, function(error, result) {
      if (error) throw error;
      console.log(result);
      return result;
    });
  }
}