[英]Use common Javascript file in Angular and nodeJS
I have a javascript file named sum.js
我有一个名为sum.js
的JavaScript文件
var addNumber = function (a,b){
return a + b;
}
exports.addNumber = addNumber;
I want to use same sum.js
file's addNumber
function in nodeJS
and in Angular 5
. 我想在nodeJS
和Angular 5
使用相同的sum.js
文件的addNumber
函数。 How can I do it. 我该怎么做。 Is there any ways to do it? 有什么办法吗?
Your help will be highly appreciated. 非常感谢您的帮助。
You can create an npm package and use it in both: 您可以创建一个npm软件包,并在两个软件包中使用它:
https://medium.freecodecamp.org/how-to-make-a-beautiful-tiny-npm-package-and-publish-it-2881d4307f78 https://medium.freecodecamp.org/how-to-make-a-beautiful-tiny-npm-package-and-publish-it-2881d4307f78
You can give the reference of your js file in angular-cli.json like below 您可以在angular-cli.json中提供js文件的引用,如下所示
"scripts": [
....
"./assets/js/sum.js"
],
and then in your .ts of angular you can directly use it by declaring it like 然后在您的angular .ts文件中,您可以像这样声明它来直接使用它
import * as sum from '../../........./sum.js' //ref of ur file
class xyz {
let a = 0,b=1
constructor() { sum.addNumber(a, b); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.