Javascript 如何将我自己的node.js脚本添加到Angular 4项目

Javascript 如何将我自己的node.js脚本添加到Angular 4项目,javascript,node.js,angular,Javascript,Node.js,Angular,我有一个文件function.js,它将函数导出为与一些api通信并返回值。它使用请求库进行通信。我正在node.js应用程序中使用此功能。如何在angular 4应用程序中导入/使用它 我想把这个文件安装为node_模块。所以我创建了一个新的文件夹函数 其中是function.js、package json和node_modules文件夹。然后我通过命令在我的ng4应用程序中安装了它 npm install --save ./function 那我就在打字机上声明 declare var

我有一个文件function.js,它将函数导出为与一些api通信并返回值。它使用请求库进行通信。我正在node.js应用程序中使用此功能。如何在angular 4应用程序中导入/使用它

我想把这个文件安装为node_模块。所以我创建了一个新的文件夹函数 其中是function.js、package json和node_modules文件夹。然后我通过命令在我的ng4应用程序中安装了它

npm install --save ./function 
那我就在打字机上声明

declare var Myfunction: any
并将其导入到我的组件中

import * as Myfunction from "function"
但当我尝试使用它时,我得到了一个错误:

ERROR in /Users/i343346/order-site/src/app/order/order.component.ts (37,12): Unexpected token. A constructor, method, accessor, or property was expected.
ERROR in /Users/i343346/order-site/src/app/order/order.component.ts (37,13): Function implementation is missing or not immediately following the declaration.
更新 我试过@Z.Bagley answear,现在我可以导入我的文件了。但有一个错误:

WARNING in ./node_modules/ajv/lib/compile/index.js 13:21-34 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (/Users/i343346/order-site/node_modules/webpack/lib/dependencies/CommonJsRequireContextDependency.js:27:4)
at Compilation.reportDependencyErrorsAndWarnings (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:677:24)
at Compilation.finish (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:535:9)
at applyPluginsParallel.err (/Users/i343346/order-site/node_modules/webpack/lib/Compiler.js:512:17)
at /Users/i343346/order-site/node_modules/tapable/lib/Tapable.js:289:11
at _addModuleChain (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:481:11)
at processModuleDependencies.err (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

WARNING in ./node_modules/ajv/lib/async.js 96:20-33 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (/Users/i343346/order-site/node_modules/webpack/lib/dependencies/CommonJsRequireContextDependency.js:27:4)
at Compilation.reportDependencyErrorsAndWarnings (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:677:24)
at Compilation.finish (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:535:9)
at applyPluginsParallel.err (/Users/i343346/order-site/node_modules/webpack/lib/Compiler.js:512:17)
at /Users/i343346/order-site/node_modules/tapable/lib/Tapable.js:289:11
at _addModuleChain (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:481:11)
at processModuleDependencies.err (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

WARNING in ./node_modules/ajv/lib/async.js 119:15-28 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (/Users/i343346/order-site/node_modules/webpack/lib/dependencies/CommonJsRequireContextDependency.js:27:4)
at Compilation.reportDependencyErrorsAndWarnings (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:677:24)
at Compilation.finish (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:535:9)
at applyPluginsParallel.err (/Users/i343346/order-site/node_modules/webpack/lib/Compiler.js:512:17)
at /Users/i343346/order-site/node_modules/tapable/lib/Tapable.js:289:11
at _addModuleChain (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:481:11)
at processModuleDependencies.err (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9) 

我认为它与请求库有关,浏览器可能不支持该库。

在Angular 4项目中包含自定义javascript函数的一种简单方法是将该项包含在资产文件夹中,然后使用
require
将其导入组件类型脚本

src/assets/example.js

export function test() {
    console.log('test');
}
src/app/app.component.ts

export function test() {
    console.log('test');
}
(在
@组件(…)
之前)

(在导出类AppComponent内部实现OnInit{…)


我认为您可能还需要通过脚本标记将代码包含在index.html中


您可能会发现另一个答案很有用:

您有一个小的输入错误。它应该是
install
…除非您在commanddeclare var函数中犯了这个错误:any不表示sencedeclare var myFunction:any;从“function”导入*作为myFunction@谢谢你,我改正了。我没有用这个错误command@Milad谢谢,我更正了它。谢谢,现在我可以导入我的文件了,但是我遇到了另一个错误。如果你的custom.js有依赖项,你必须显式地包含它们。如果你的custom.js没有,那么这是一个不相关的错误。它使用request,我将其添加到我的dependencie中s、 但它仍然不起作用。我更新了关于新错误的问题。
ngOnInit() {
  example.test();
}