【问题标题】:Add plugins to the angular-cordova Angular plugin将插件添加到 angular-cordova Angular 插件
【发布时间】:2019-08-23 15:04:39
【问题描述】:

我已经使用 Angular 创建我的项目,并且效果很好。

现在我正在尝试将它包装在一个 Cordova 项目中,但我不知道如何在 de Angular 代码中使用我需要的一些 Cordova 插件...

我该怎么办?我正在尝试使用 cordova-plugin-android-volume 设置设备的音量(我只对 android 感兴趣)

【问题讨论】:

  • 请张贴minimal reproducible example 以及到目前为止您所尝试的内容。 API 文档是here
  • 我已经安装了它并且我已经清楚如何在 Cordova 中使用它......但我需要在 Angular .ts 文件中使用它。我想这样做是因为我的应用程序是用 angular 编写的并包裹在 cordova 中,所以我不能直接在 angular 脚本中使用 cordova 插件

标签: angular cordova plugins


【解决方案1】:

您可以使用添加cordova-plugin-android-volume

cordova plugin add https://github.com/MCluck90/cordova-plugin-android-volume.git

现在,由于您使用的是 Angular 和 Typescript,因此您需要先声明 cordovaandroidVolume 才能使用。使用declare关键字查看下面的代码

import { Component } from '@angular/core';

declare var cordova: any;
declare var window: any;

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  setVolume() {
    console.log(cordova);
    console.log(window.androidVolume);
  }
}

【讨论】:

  • 它给了我一个错误:ERROR ReferenceError: cordova is not defined. 如果我尝试只打印 console.log(window.androidVolume); 它返回我“未定义”
  • 您是否在 index.html 中包含了 cordova.js?
  • 好的,现在 Cordova 可以工作了……抱歉。但是console.log(window.androidVolume); 给了我“未定义”,如果我尝试使用window.androidVolume.setAll(100); 设置音量,它会给我错误“无法读取未定义的属性'setAll'”
猜你喜欢
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多