简体   繁体   English

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

[英]How to add my own node.js script to Angular 4 project

I have a file function.js which exports function to comunicate with some api and return value. 我有一个文件function.js,它导出函数以与一些api通讯并返回值。 It comunicates using request library. 它使用请求库进行通信。 I am using this in node.js app. 我在node.js应用程序中使用它。 How can I import/use it in my angular 4 app? 如何在我的angular 4应用程序中导入/使用它?

I thought to install this file as node_module. 我以为将此文件安装为node_module。 So I created new folder function where is function.js, package-json and node_modules folder. 因此,我创建了新的文件夹函数,即function.js,package-json和node_modules文件夹。 and then I installed it in my ng4 app by command 然后通过命令将其安装在ng4应用中

npm install --save ./function 

then in typings.d.ts I declare it 然后在types.d.ts中声明

declare var Myfunction: any

and import it in my component 并将其导入我的组件中

import * as Myfunction from "function"

but when i try using it I get an error : 但是当我尝试使用它时,我得到一个错误:

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.

UPDATE I tried @Z. 更新我尝试了@Z。 Bagley answear and now i can import my file. Bagley answear,现在我可以导入文件了。 but got error: 但出现错误:

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) 

I think is connected with request library, which maybe isn't supported by browser. 我认为是与请求库连接的,浏览器可能不支持。

A simple way to include custom javascript functions in your Angular 4 project is to include the item in your assets folder, and then use require to import it into your component typescript. 在Angular 4项目中包含自定义javascript函数的一种简单方法是将该项目包含在资产文件夹中,然后使用require将其导入到组件打字稿中。

src/assets/example.js SRC /资产/ example.js

export function test() {
    console.log('test');
}

src/app/app.component.ts SRC /应用程序/ app.component.ts

(before @Component(...) ) (在@Component(...)之前)

declare var require: any;
const example = require('../assets/example');

(inside export class AppComponent implements OnInit { ... ) (在export class AppComponent implements OnInit { ...内部export class AppComponent implements OnInit { ...

ngOnInit() {
  example.test();
}

I think you might also need to include your code in index.html via script tag. 我认为您可能还需要通过脚本标记将代码包含在index.html中。

You might find this other answer useful: https://stackoverflow.com/posts/40635697/edit 您可能会发现此其他答案很有用: https : //stackoverflow.com/posts/40635697/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM