TypeScript 和 Chrome 通知 [英] TypeScript and Chrome Notification

查看:68
本文介绍了TypeScript 和 Chrome 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建 Chrome 应用.该应用程序是用 TypeScript (Angular2) 编写的.

I'm building a Chrome app. The app is written with TypeScript (Angular2).

我想推送通知.代码如下:

I would like to push notifications. Here's the code :

import {Injectable} from 'angular2/core';

@Injectable()
export class NotificationService {
    constructor() {
        if(Notification.permission !== 'granted') {
            Notification.requestPermission();
        }
    }
}

gulp 构建应用程序时,它说:

When gulp build the app, it says :

src/scripts/stream/notification.service.ts(6) Cannot find name 'Notification'.

我试图将我的课程包裹在里面:

I tried to wrap my class inside :

/* tslint:disable */
... the code
/* tslint:enable */

但它不会改变任何东西.

But it does not change anything.

有没有办法用 tslint.json 文件告诉 Typescript 这是一个全局变量?

Is there a way with tslint.json file to tell Typescript that this is a global variable ?

使用 jshint 会是这样:

With jshint it would be something like that :

"globals": {
   "Notification": false
}

推荐答案

这里有几个选项:

  1. 将以下行添加到文件顶部.

  1. Add the following line to the top of your file.

声明 var 通知:任何;

declare var Notification: any;

这将使转译器通过,但不会为您提供 TypeScript 的大部分功能.

This will make the transpiler pass but won't give you the much of TypeScript's features.

下载定义类型 (chrome.d.ts).

Download the definition type (chrome.d.ts).

TSD 似乎是最受欢迎的定义管理器.
Typings 是另一种很有前景的替代方案.

TSD seems to be the most popular definition manager.
Typings is another promising alternative.

这篇关于TypeScript 和 Chrome 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆