TypeScript和Angular 2的导航器和窗口类型错误 [英] Navigator and window type error with TypeScript and Angular 2

查看:63
本文介绍了TypeScript和Angular 2的导航器和窗口类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Ionic 2应用程序中使用ng2-translate,

I am trying to use ng2-translate in my Ionic 2 app, with:

this.translateService.setDefaultLang('en');
            if (this.platform.is('cordova')) {
                if (navigator.globalization) {
                    navigator.globalization.getPreferredLanguage(function (language) {

但我收到此错误

Typescript Error
Property 'globalization' does not exist on type 'Navigator'.
src/app/app.component.ts
if (this.platform.is('cordova')) {
    if (navigator.globalization) {

在这行代码中,我与window.cordova的错误类型相同:

And I have the same type of error with window.cordovain this line of code:

if (this.platform.is('cordova') && window.cordova.plugins.Keyboard) {

我应该声明导航器类型还是什么?

Should I declare navigator type or something ?

推荐答案

您缺少该导航器对象的Typescript输入信息.基本上,您看到的错误是Typescript编译器在说,根据您到目前为止提供的类型定义,没有信息说明navigator对象具有globalization属性.

You're missing Typescript typing information for that navigator object. Basically, the error you're seeing is the Typescript compiler saying that, based on the type definitions you have supplied so far, there is no information stating that the navigator object has a globalization property.

解决方案是为该导航器对象添加类型信息.

The solution is to add the typing information for that navigator object.

老实说,我不确定哪个包包含那些类型定义,但是从Typescript v2(或v2.1 ...不记得哪个)中,npm将Typescript库的类型定义存储在@types命名空间下.

I'm honestly not sure which package contains those type definitions, but from Typescript v2 (or v2.1... can't remember which), npm stores type definitions for Typescript libraries under the @types namespace.

所以,你想

npm install --save-dev @types/<some package>

将类型定义添加到您的项目中.

to add the type definitions into your project.

我会从以下内容开始猜测:

I'd take a guess at starting with:

@npm install --save-dev @types/cordova-ionic 

这篇关于TypeScript和Angular 2的导航器和窗口类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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