简体   繁体   English

如何将 Javascript 导入 Angular2 中的全局命名空间

[英]How to import Javascript into global namespace in Angular2

This question has been asked in different versions repeatedly, but none of the solutions I found fit to the specific case I'm trying to solve, so please bear with me.这个问题已经在不同的版本中被反复问过,但我发现没有一个解决方案适合我试图解决的具体案例,所以请多多包涵。

I'm using Angular2.我正在使用 Angular2。 I'm trying to add a series of Javascript-Files, so that their contents are available to a component.我正在尝试添加一系列 Javascript 文件,以便它们的内容可用于组件。 Specifically, I'm trying to import the angular.js demo files from the Wacom Signature SDK.具体来说,我正在尝试从 Wacom Signature SDK 导入 angular.js 演示文件。

There are three Javascript files which are throwing a series of variables and functions into the globale namespace.有三个 Javascript 文件将一系列变量和函数扔到 globale 命名空间中。 For example例如

SigCaptX-Globals.js SigCaptX-Globals.js

var wgssSignatureSDK = null;  // Signature SDK object
var sigObj = null;            // Signature object
var sigCtl = null;            // Signature Control object
var dynCapt = null;           // Dynamic Capture object
var wizCtl = null;            // Wizard object
var scriptIsRunning = false;  // script run status
var pad = null                // created on script start, sets pad control data from class CPadCtl
var inputObj = null;  

I have tried adding the files to the "scripts" definition in angular.json.我尝试将文件添加到 angular.json 中的“脚本”定义中。 Compilation works, so I think it's finding the files.编译有效,所以我认为它正在查找文件。 But I have no clue how to make it so that I have access to the Billion global namespace variables and functions defined within these files.但我不知道如何制作它,以便我可以访问这些文件中定义的 Billion 全局命名空间变量和函数。

Please explain it to me like I'm a child;请像我是个孩子一样向我解释; I'm not smart.我不聪明。

TLDR: TLDR:

From what you stated, you should be able to access the variables from your components, pipes, services, etc, directly from the window object, like, window.wgssSignatureSDK .根据您的说明,您应该能够直接从window object 访问组件、管道、服务等中的变量,例如window.wgssSignatureSDK

Long answer长答案

The first thing you need to do, is adding the script to the runtime.您需要做的第一件事是将脚本添加到运行时。 Since you added the files to the angular.json file in the scripts properties, the JS files should be downloaded to the browser when the app starts and included in the HTML, effectively loading it to the JS runtime.由于您在scripts属性中将文件添加到angular.json文件中,因此应在应用程序启动时将 JS 文件下载到浏览器并包含在 HTML 中,从而有效地将其加载到运行时。

Note: The app must be recompiled when changes happen to the angular.json file.注意:angular.json文件发生更改时,必须重新编译应用程序。

Now, since the file is already loaded, technically, you could access the variables through the window global object, like, window.wgssSignatureSDK .现在,由于文件已经加载,从技术上讲,您可以通过window全局 object 访问变量,例如window.wgssSignatureSDK Below there is an example of a JS lib.下面是一个 JS 库的示例。

// Your or any third party library
var YourLib = {
  // It can have variables, constants
  PI: Math.PI,

  // It can have complex objects
  person: {
    name: 'John Doe',
    address: {
      street: 'No name',
      number: 10
    }
  },

  // It can also have functions
  add: function(a, b) {
    return a + b;
  },

}

Note: See that, it is important that you scope your variables and functions, so they don't live directly into the window object, so instead of using like this window.wgssSignatureSDK , we would use it like this window.YourLib.wgssSignatureSDK . Note: See that, it is important that you scope your variables and functions, so they don't live directly into the window object, so instead of using like this window.wgssSignatureSDK , we would use it like this window.YourLib.wgssSignatureSDK . This will help to avoid conflicts with other existing libraries and future additions to the window object.这将有助于避免与其他现有库的冲突以及未来对 window object 的添加。

However, to implement it the Angular way, we should avoid accessing the DOM and global variables directly, for that, we could make use of the dependency injection mechanism.但是,要实现它 Angular 的方式,我们应该避免直接访问 DOM 和全局变量,为此,我们可以利用依赖注入机制。 Create a file to expose tokens to be used by the dependency injection mechanism.创建一个文件以公开依赖注入机制要使用的tokens It should have at least one token per lib.每个库应该至少有一个令牌。

your-tokens-file.ts你的令牌文件.ts

import { InjectionToken } from "@angular/core";

export const YourLibTokenOne = new InjectionToken('YOUR_LIB_1');
export const YourLibTokenTwo = new InjectionToken('YOUR_LIB_2');

app.module.ts app.module.ts

// At the top of the file
import { YourLibTokenOne, YourLibTokenTwo } from './your-tokens-file';

const YourLibOne = (window as any).YourLibOne;
const YourLibTwo = (window as any).YourLibTwo;

// The module definition
@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    // In here we are provinding to the injectors a value for a specific token
    { provide: YourLibTokenOne, useValue: YourLibTokenOne },
    { provide: YourLibTokenTwo, useValue: YourLibTokenTwo }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Now the libs are provided to the dependency injection mechanism, we now just need to inject it into the components, services, directives or any Angular class mechanism.现在库提供给依赖注入机制,我们现在只需将其注入到组件、服务、指令或任何 Angular class 机制中。

your-component.ts你的组件.ts

import { YourLibTokenOne } from './your-tokens-file';

@Component({ ... })
export class YourComponent {

  constructor(@Inject(YourLibTokenOne) public yourLibOne: any) {
    console.log(yourLibOne.PI);
    console.log(yourLibOne.add(10, 20));
  }

}

Please explain it to me like I'm a child;请像我是个孩子一样向我解释; I'm not smart.我不聪明。

No one knows everything, life is a learning journey, so don't be too hard on yourself:)没有人知道一切,人生就是一场学习之旅,所以不要对自己太苛刻:)

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

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