typescript 在打字稿中使用 chrome 扩展 apis

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28844406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:58:02  来源:igfitidea点击:

Using chrome extension apis in typescript

javascriptgoogle-chromegoogle-chrome-extensiontypescriptwebstorm

提问by Tristan Djahel

I'm building a chrome extension written in TypeScript. I'm using WebStorm and I added the chrome-DefiniteltyTypedlibrary in my project.

我正在构建一个用 TypeScript 编写的 chrome 扩展。我正在使用 WebStorm,并chrome-DefiniteltyTyped在我的项目中添加了该库。

However, when I write this in my typescript code : chrome.extension.getURLI got an error : cannot find name 'chrome'.

但是,当我在我的打字稿代码中编写此代码时:chrome.extension.getURL出现错误:cannot find name 'chrome'

Because of this, my javascript file is not generated and I cannot use it in my extension.

因此,我的 javascript 文件没有生成,我无法在我的扩展中使用它。

Do you guys have any solution?

各位有什么解决办法吗?

采纳答案by basarat

That should work fine : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/chrome/index.d.ts

这应该可以正常工作:https: //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/chrome/index.d.ts

TIP: make sure you have a referencetag added:

提示:确保您reference添加了标签:

/// <reference path="pathTo/chrome.d.ts"/>

/// <reference path="pathTo/chrome.d.ts"/>

回答by dfl

As of typescript 2 (or 2.x, not sure), you should import the chrome types from @types.

从 typescript 2(或 2.x,不确定)开始,您应该从 @types 导入 chrome 类型。

in package.json:

在 package.json 中:

"devDependencies": {
    ...
    "@types/chrome": "0.0.35", // or just npm install --save-dev @types/chrome

And in tsconfig:

在 tsconfig 中:

    "types": [
        //(various types, e.g. jquery, core-js),
        "chrome"
    ]