打字稿错误:找不到名称“cordova”
Posted
技术标签:
【中文标题】打字稿错误:找不到名称“cordova”【英文标题】:Typescript Error: Cannot find name 'cordova' 【发布时间】:2017-10-24 09:22:10 【问题描述】:import Component from '@angular/core';
import NavController, Platform, AlertController from 'ionic-angular';
import Transfer, TransferObject from '@ionic-native/transfer';
import File from '@ionic-native/file';
@Component(
selector: 'page-about',
templateUrl: 'about.html',
providers: [Transfer, TransferObject, File]
)
export class AboutPage
storageDirectory: string = '';
constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController)
this.platform.ready().then(() =>
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova'))
return false;
if (this.platform.is('ios'))
this.storageDirectory = cordova.file.documentsDirectory;
else if(this.platform.is('android'))
this.storageDirectory = cordova.file.externalDataDirectory;
console.log(this.storageDirectory);
else
// exit otherwise, but you could add further types here e.g. Windows
return false;
);
downloadImage()
this.platform.ready().then(() =>
const fileTransfer: TransferObject = this.transfer.create();
const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';
fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) =>
const alertSuccess = this.alertCtrl.create(
title: `Download Succeeded!`,
subTitle: `successfully downloaded to: $entry.toURL()`,
buttons: ['Ok']
);
alertSuccess.present();
, (error) =>
const alertFailure = this.alertCtrl.create(
title: `Download Failed!`,
subTitle: `was not downloaded. Error code: $error`,
buttons: ['Ok']
);
alertFailure.present();
);
);
I am getting the error attached in screenshot.我在 ionic 2 中运行我的项目构建时遇到错误,尽管我已经使用以下命令安装了“打字”
npm install -g typings typings, install dt~cordova --save --global
并尝试了所有可能的方法来消除此错误,检查了所有 Cordova 插件,如文件、文件传输,但仍然无法解决错误。
任何人都可以找到它。
这里还附上了代码,我不知道我哪里出错了..
【问题讨论】:
ionicframework.com/docs/native 你应该使用 ionic native.. 不推荐使用类型 【参考方案1】:我已经编辑了您的代码,添加了declare let cordova: any;
这公开了科尔多瓦 api 以供使用。希望这会有所帮助。
import Component from '@angular/core';
import NavController, Platform, AlertController from 'ionic-angular';
import Transfer, TransferObject from '@ionic-native/transfer';
import File from '@ionic-native/file';
declare let cordova: any;
@Component(
selector: 'page-about',
templateUrl: 'about.html',
providers: [Transfer, TransferObject, File]
)
export class AboutPage
storageDirectory: string = '';
constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController)
this.platform.ready().then(() =>
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova'))
return false;
if (this.platform.is('ios'))
this.storageDirectory = cordova.file.documentsDirectory;
else if(this.platform.is('android'))
this.storageDirectory = cordova.file.externalDataDirectory;
console.log(this.storageDirectory);
else
// exit otherwise, but you could add further types here e.g. Windows
return false;
);
downloadImage()
this.platform.ready().then(() =>
const fileTransfer: TransferObject = this.transfer.create();
const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';
fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) =>
const alertSuccess = this.alertCtrl.create(
title: `Download Succeeded!`,
subTitle: `successfully downloaded to: $entry.toURL()`,
buttons: ['Ok']
);
alertSuccess.present();
, (error) =>
const alertFailure = this.alertCtrl.create(
title: `Download Failed!`,
subTitle: `was not downloaded. Error code: $error`,
buttons: ['Ok']
);
alertFailure.present();
);
);
【讨论】:
【参考方案2】:您必须在src/declarations.d.ts
文件中声明cordova
命名空间,以便打字稿转译器可以理解cordova 已声明并引用对象,但在您的情况下,如果您想使用插件,最好使用ionic-native如果插件没有列出,请声明插件的命名空间并使用它。
【讨论】:
以上是关于打字稿错误:找不到名称“cordova”的主要内容,如果未能解决你的问题,请参考以下文章
Ionic Cordova Build prod:找不到模块“。” - 打字稿版本>3
打字稿错误使用googlemaps javascript API时在ionic2中找不到名称'google'