gpt4 book ai didi

javascript - 将脚本添加到 Angular 库

转载 作者:行者123 更新时间:2023-12-01 14:51:06 25 4
gpt4 key购买 nike

我正在尝试将 Angular 6 应用程序的一些组件导出到库中。不幸的是,我需要使用 WebToolkit 连接到由其他人构建的专有服务,该服务只能作为纯 javascript 文件使用。这个文件,反过来也需要jQueryrequire.js .

没有库,我通过将这些 js 文件添加到 .angular-cli.json 来解决这个问题。在 "scripts"

{
...,
scripts: [
"node_modules/jquery/dist/jquery.js",
"node_modules/require/require.js",
"path/to/my/magic/library.js"
],
...
}

现在构建我的 Angular 库,我希望将这些脚本构建到我自己的库代码中,这样我的客户仍然可以通过执行一个 npm-install 并将其导入到他们的 .module.ts 中的简单方式使用它。文件。

Angular-CLI 6 有可能吗?或者您有其他建议如何实现我的库的简单安装?

最佳答案

   Provide the reference of your external JS in your angular. json file of main angular project in a script tag and provide the path of your package from your libraries node_modules folder like this. So now you have created the NPM package from your library and you are going to use it in different project

**Add externaljs file**
**Step 1**
Take a file in the assets folder and set any name. Like as custom.js

function getToday() {
alert(new Date().toLocaleDateString());
}

function greetings(name) {
alert(`wellcome ${name}`);
}


**Step 2**
Now add the customjs file reference in the angular.json scripts section array list. Like as below


"src/assets/custom.js"

**Step 3**
Now add the functions reference in a component file. I am using the app component.ts

//external js function declaration
declare function getToday(): any;
declare function greetings(name: any): any;
ngOnInit(): void {
// call the externaljs functions
getToday(); // without param
greetings('rohol'); // with param
}


关于javascript - 将脚本添加到 Angular 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50990388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com