它只是在你的组件中添加declare var AirConsole 来使用它,一个傻瓜组件
import { Component, VERSION } from '@angular/core';
declare var AirConsole //<--this is the "magic"
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
airConsole=new AirConsole() //<--now you can use airConsole in your component
}
更新好吧,一般我们可以使用一个服务来管理airConsole,这让我们所有注入服务的组件都可以使用函数,变量,...
声明:我不知道“AirConsole”,所以我想控制它就像我们可以控制其他 .js(如 Cordova)一样)
由于我们需要 Angular 知道何时在 .js 中执行函数,我们的服务可以像
import { Injectable,NgZone } from '@angular/core';
declare var AirConsole;
@Injectable({
providedIn: 'root',
})
export class AirConsoleService implements OnInit {
airConsole=new AirConsole()
message:Subject=new Subject<any>();
constructor(private ngZone: NgZone) { }
ngOnInit(){
this.airconsole.onMessage = (from, data)=>{
this.ngZone.run(() => {
this.message.next({from:from,data:data})
});
})
}
message(device:any,message:any){
this.ngZone.run(() => {
this.airConsole.message(device,message);
});
}
}
所以,例如你可以订阅airConsoleService.message