简体   繁体   English

如何将服务/组件属性发送到 Angular 中的 vanilla JS 文件?

[英]How to send a service/component property to a vanilla JS file in Angular?

In an Angular project, I've got a third party library I'm trying out, but it needs some tweaks to work how I want it to.在一个 Angular 项目中,我有一个正在尝试的第三方库,但它需要一些调整才能按照我的意愿工作。 In one of the library's JS files, I need the ability to dynamically construct a URL using a property received from my Angular service (or, perhaps from a component).在库的一个 JS 文件中,我需要能够使用从我的 Angular 服务(或者,可能来自组件)接收的属性动态构造 URL。

Is there any way I can import an Angular service's property into a vanilla JS file and use it?有什么方法可以将 Angular 服务的属性导入 vanilla JS 文件并使用它?

I will try answering given the information you provided鉴于您提供的信息,我将尝试回答

If you can, I would advise having the external JS file as a dependency of your angular's service and not the opposite.如果可以的话,我建议将外部 JS 文件作为 Angular 服务的依赖项,而不是相反。

But this is not what you are asking and if you cannot, injecting an angular service into an external JS is quite cumbersome because you need the whole injection context to do so.但这不是您要问的,如果您不能,将 Angular 服务注入外部 JS 非常麻烦,因为您需要整个注入上下文来执行此操作。

In your situation, if the library you want to use is kind of monolithic or hard to use as a dependency and if you can change some code into it, then you could possibly rely on a mediator .在您的情况下,如果您要使用的库是单一的或难以用作依赖项,并且如果您可以将一些代码更改为它,那么您可能会依赖mediator

A mediator is a class known to anyone, and anyone can use it to send to or get information from.调解员是任何人都知道的一类,任何人都可以使用它来发送信息或从中获取信息。 In your case, you could have a plain JS mediator so that you can import it from your plain JS file too.在你的情况下,你可以有一个普通的 JS 中介,这样你也可以从你的普通 JS 文件中导入它。

But normally you do have your angular SPA and unless you do have events coming from your library, you should have the hand on who triggers what and when.但通常你确实有你的角度 SPA,除非你确实有来自你的库的事件,否则你应该知道谁触发了什么以及何时触发。 So in the case of an event, I do not see any problem of sending your property's data to your library.因此,如果发生事件,我认为将您的财产数据发送到您的图书馆没有任何问题。

In the case where your library emits events by its own, either watch for those events within angular, build your url and send the data back to the library (be careful to use an ngZone in that case)如果您的库自己发出事件,请在 Angular 中观察这些事件,构建您的 url 并将数据发送回库(在这种情况下请小心使用ngZone

If your library must handle events on its own and there is no way to have the hand on it, perhaps you should just pass a callback to get the data you need to provide to this library, that callback will provide the mandatory data on time to the lib.如果您的库必须自己处理事件并且没有办法处理它,也许您应该只传递一个回调来获取您需要提供给该库的数据,该回调将按时提供强制性数据以库。 (I've no detail of the library and therefore cannot help much). (我没有图书馆的细节,因此帮不上什么忙)。 Here is how it could work:以下是它的工作原理:

import MyLib from './myLib.js' 

@Injectable({ providedIn: 'root' })
export class MyService {

private myLib: MyLib;

constructor(){
   this.myLib = new MyLib();
   this.myLib.onTask(()=>this.getUrl())
}

private getUrl(): string {
return "www.urlFromAService.com"
}
}

And if you really do not have any callback, you could store a callback on the object's instance如果你真的没有任何回调,你可以在对象的实例上存储一个回调

this.myLib.getUrl = ()=>this.getUrl();

so that within your js code, your instance will have a getUrl function using the service's method (and context), while this is possible, it is hackish, and I would only advise you to do this for a POC.这样在您的 js 代码中,您的实例将有一个使用服务方法(和上下文)的getUrl函数,虽然这是可能的,但它是 hackish,我只建议您为 POC 执行此操作。

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

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