我目前正在使用Angular 8,并尝试使用某人脚本。我遇到一个问题,并且对此问题缺乏知识。
在javascript中,其功能如下:
window.X = function() {
var e = argument.length > 0...
如何将值传递到Typescript中的此Javascript函数中?
要使用正确的类型处理TypeScript中的外部函数,只需在使用它之前在某个位置声明它即可。在TypeScript变量中,将参数长度键入为(...args: any[])
(或在所有参数均属于同一类型的情况下键入更严格的类型)。
例:
<script>
// function defined in index.html
window.X = function() {
console.log('X', arguments.length);
}
</script>
// angular component
import { Component } from '@angular/core';
// declare external function with proper type
declare const X: (...args: any[]) => void;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
run(): void{
// call the external function with multiple parameters
X('a', 'b');
}
}
这是一个有效的stackblitz:https ://stackblitz.com/edit/angular-ivy-zdr8ca ? file = src%2Findex.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句