Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将TypeScript与PhoneGap/Cordova插件一起使用_Cordova_Typescript_Phonegap Plugins - Fatal编程技术网

将TypeScript与PhoneGap/Cordova插件一起使用

将TypeScript与PhoneGap/Cordova插件一起使用,cordova,typescript,phonegap-plugins,Cordova,Typescript,Phonegap Plugins,我遇到了一些障碍,因为我想开始使用,但我正在用TypeScript编写我的所有javascript资产。下面是我遇到的具体问题的一个很好的例子: window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID("UA-xxxxxxx-x"); TypeScript在窗口处停止。插件,引发编译错误: 类型为“Window”的值上不存在属性“plugins” 这完全有道理。然而,我不能通过使用declarevar窗口来回避这个问题语句,

我遇到了一些障碍,因为我想开始使用,但我正在用TypeScript编写我的所有javascript资产。下面是我遇到的具体问题的一个很好的例子:

window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID("UA-xxxxxxx-x");
TypeScript在
窗口处停止。插件
,引发编译错误:

类型为“Window”的值上不存在属性“plugins”


这完全有道理。然而,我不能通过使用
declarevar窗口来回避这个问题语句,因为它为窗口创建了重复的标识符

第一步是扩展
窗口
界面,您可以这样做:

interface Window {
    plugins: any;
}
这意味着没有编译器错误,但除非扩展定义,否则这意味着没有自动完成。所以这条线现在可以工作了:

window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID("UA-xxxxxxx-x");
为了让事情更上一层楼,让自己自动完成(也为了检查拼写),你可以使用这个扩展版本的定义

interface GoogleAnalyticsPlugin {
    startTrackerWithAccountID(accountId: string): void;
}

interface Plugins {
    googleAnalyticsPlugin: GoogleAnalyticsPlugin;
}

interface Window {
    plugins: Plugins;
}