如何在angular-cli 1.0.1应用中使用chrome-app.d.ts类型? [英] How to use chrome-app.d.ts type in angular-cli 1.0.1 app?
问题描述
我已经安装了 @ types/chrome 并将其添加到 tsconfig.app.json
I've installed @types/chrome and add it in tsconfig.app.json
"types": [
"chrome"
]
但是当我在组件中使用chrome.app
时,它会显示错误:
But when i use chrome.app
in component, it show an error:
类型"chrome"上不存在属性"app"
Property 'app' does not exist on type 'typeof chrome'
@ types/chrome 文件夹包含 chrome-app.d.ts 是我需要的文件,我该如何参考
@types/chrome folder contains chrome-app.d.ts is a file which i need, how can I refer to this
来源 https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/chrome
推荐答案
由于此类型定义模块的创建者选择不将chrome.app.*
定义包括在@types/chrome
模块的index.d.ts
文件中,而是单独它们放入chrome-app.d.ts
中,您需要在使用这些API的文件中明确引用它:
Since the creators of this typings definition module have chosen not to include the chrome.app.*
definitions in the index.d.ts
file of the @types/chrome
module but instead separate them out into chrome-app.d.ts
, you'll need to explicitly reference it in the file that uses those APIs:
/// <reference types="chrome/chrome-app"/>
请注意,根据TypeScript中三斜杠指令的文档,该文件必须位于文件的顶部:
Note that this must be at the top of the file, per the documentation for triple-slash directives in TypeScript:
https://www.typescriptlang.org/docs/handbook/Triple-slash-directives.html
还请注意,不必 在您的tsconfig.json
文件中添加"types": ["chrome"]
引用,因为TypeScript编译器的最新版本应自动导入.在node_modules/@types
中找到的所有类型定义模块.通过将其添加到您的配置中,您就是在告诉它 only 导入那个模块.有关更多信息,您可以参考配置文档:
Also note that it should not be necessary to add a "types": ["chrome"]
reference in your tsconfig.json
file, as the latest version of the TypeScript compiler should automatically import all typings definition modules found in node_modules/@types
. By adding this to your config, you are telling it to only import that one module. For more on this, you can refer to the config docs:
https://www.typescriptlang.org/docs/handbook/tsconfig- json.html
这篇关于如何在angular-cli 1.0.1应用中使用chrome-app.d.ts类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!