Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic生成找不到模块错误。如何导入Android插件?_Android_Angular_Cordova_Ionic Framework_Ionic4 - Fatal编程技术网

Ionic生成找不到模块错误。如何导入Android插件?

Ionic生成找不到模块错误。如何导入Android插件?,android,angular,cordova,ionic-framework,ionic4,Android,Angular,Cordova,Ionic Framework,Ionic4,我正在尝试开发一个安卓插件,用于Ionic应用程序 我用爱奥尼亚已经提供的初学者项目创建了这个应用程序,使用的是爱奥尼亚开始我的应用程序选项卡 我还在另一个文件夹中创建了我的插件,其中包含以下文件 plugin.xml GSLocationManager.java 然后我为我的插件usignplugman创建了一个package.json 在那之后,通过做以下操作将我的android插件添加到我的Ionic项目中 爱奥尼亚科尔多瓦插件添加路径/到/my/plugin 但是我似乎无法使用impor

我正在尝试开发一个安卓插件,用于Ionic应用程序

我用爱奥尼亚已经提供的初学者项目创建了这个应用程序,使用的是
爱奥尼亚开始我的应用程序选项卡

我还在另一个文件夹中创建了我的插件,其中包含以下文件

plugin.xml

GSLocationManager.java

然后我为我的插件usignplugman创建了一个package.json 在那之后,通过做以下操作将我的android插件添加到我的Ionic项目中 爱奥尼亚科尔多瓦插件添加路径/到/my/plugin

但是我似乎无法使用
import{GSLocationManager}从“globespinning location plugin”导入我的插件到
app.module.ts
我尝试构建,但CLI在src/app/app.module.ts(12,31)中显示
错误:错误TS2307:找不到模块“全局规划位置插件”。

知道错误在哪里吗? 我应该如何导入由我创建并从本地文件夹引用的插件?
我希望能够在我的ionic应用程序中调用
GSLocationManager.getCurrentLocation()

如果导入失败,这很可能与您的tsconfig.json或角度模块(app.module.ts)有关。检查文件是否包含在中


您只发布了Java文件。您确定这在浏览器上运行吗?

您想继续运行
爱奥尼亚cordova插件添加“自定义插件的文件夹路径”
,然后在
.ts
文件中使用它,您想声明以下内容,
声明var myPlugin:any

声明插件后,您可以按如下方式继续使用它:

myPlugin.myFuntion((data) => {
        console.log(data);
},(err) => {
        console.log(err);
});
因此,在您的案例中,您将使用以下内容:

declare var GSLocationManager: any;

....

GSLocationManager.getCurrentLocation().then((data) => {});
除了答案,这里还有一些东西需要注意

  • 在使用
    import*from*语法之前,需要为插件js创建一个声明(.d.ts)文件。在这种情况下,不需要tho
  • 检查您是否准确地定义了js方法的名称空间。你的在这里
因此,以下是如何在您的案例中实施此功能

在.TS文件中,您希望执行该功能

// at the top of the file. after all the import statements
import { Component } from '@angular/core';  // i'm using this as an example.
import { Platform } from '@ionic/angular';  // you need the platform

declare var LocationManager;
...
constructor(
private platform: Platform
){
  this.platform.ready().then(() => {
   // we will make use of the plugin here
   console.log('LocationManager: ', LocationManager); // we can check if it exist by logging it out to the console
   if (LocationManager) {
      LocationManager.GSLocationManager.getCurrentLocation(
         success => {
            console.log('LocationManager: Got Location: ', success);
         }, error => {
            console.log('LocationManager: Err Getting Location: ', error);
         }
      );
    }
  });
}
您可以在此处阅读更多内容: ,
什么@Tachyon和@SeunBincom的答案很有帮助,但问题在于我的
plugin.xml
文件。在Android Studio的帮助下,我能够调试它,并最终使其工作

xml最终是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        id="globespinning-location-plugin" version="0.0.1">
    <name>LocationManager</name>
    <description>Globespinning Location Plugin</description>
    <license>MIT</license>
    <keywords>cordova,device,sensors,location</keywords>

    <js-module name="LocationManager" src="www/locationManager.js">
        <clobbers target="LocationManager" />
    </js-module>

    <platform name="android">
      <preference name="GOOGLE_PLAY_SERVICES_VERSION" default="11+"/>
      <preference name="ANDROID_SUPPORT_LIBRARY_VERSION" default="26+"/>
      <preference name="ICON" default="@mipmap/icon" />
      <preference name="SMALL_ICON" default="@mipmap/icon" />
      <preference name="ACCOUNT_NAME" default="@string/app_name" />
      <preference name="ACCOUNT_LABEL" default="@string/app_name" />
      <preference name="ACCOUNT_TYPE" default="$PACKAGE_NAME.account" />
      <preference name="CONTENT_AUTHORITY" default="$PACKAGE_NAME" />

      <framework src="com.google.android.gms:play-services-location:$GOOGLE_PLAY_SERVICES_VERSION" />
      <framework src="com.android.support:support-v4:$ANDROID_SUPPORT_LIBRARY_VERSION" />
      <framework src="com.android.support:appcompat-v7:$ANDROID_SUPPORT_LIBRARY_VERSION" />
      <framework src="src/android/dependencies.gradle" custom="true" type="gradleReference"/>

      <source-file src="src/android/com/globespinning/ionic/locationmanager/GSLocationManager.java"
                   target-dir="src/com/globespinning/ionic/locationmanager"></source-file>

      <config-file target="AndroidManifest.xml" parent="/manifest">
         <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
         <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
         <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
         <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
         <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
         <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
         <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
         <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
         <uses-permission android:name="android.permission.INTERNET" />
         <uses-permission android:name="android.permission.WAKE_LOCK" />
         <uses-permission android:name="android.hardware.location" />
      </config-file>

      <config-file target="res/xml/config.xml" parent="/*">
          <feature name="LocationManager">
              <param name="android-package" value="com.globespinning.ionic.locationmanager.GSLocationManager" />
          </feature>
      </config-file>
    </platform>
</plugin>

位置经理
全局规划位置插件
麻省理工学院
cordova、设备、传感器、位置

我只发布了1个java文件。我发布了插件的文件。我不确定js文件是否正确。
declare var GSLocationManager: any;

....

GSLocationManager.getCurrentLocation().then((data) => {});
// at the top of the file. after all the import statements
import { Component } from '@angular/core';  // i'm using this as an example.
import { Platform } from '@ionic/angular';  // you need the platform

declare var LocationManager;
...
constructor(
private platform: Platform
){
  this.platform.ready().then(() => {
   // we will make use of the plugin here
   console.log('LocationManager: ', LocationManager); // we can check if it exist by logging it out to the console
   if (LocationManager) {
      LocationManager.GSLocationManager.getCurrentLocation(
         success => {
            console.log('LocationManager: Got Location: ', success);
         }, error => {
            console.log('LocationManager: Err Getting Location: ', error);
         }
      );
    }
  });
}
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        id="globespinning-location-plugin" version="0.0.1">
    <name>LocationManager</name>
    <description>Globespinning Location Plugin</description>
    <license>MIT</license>
    <keywords>cordova,device,sensors,location</keywords>

    <js-module name="LocationManager" src="www/locationManager.js">
        <clobbers target="LocationManager" />
    </js-module>

    <platform name="android">
      <preference name="GOOGLE_PLAY_SERVICES_VERSION" default="11+"/>
      <preference name="ANDROID_SUPPORT_LIBRARY_VERSION" default="26+"/>
      <preference name="ICON" default="@mipmap/icon" />
      <preference name="SMALL_ICON" default="@mipmap/icon" />
      <preference name="ACCOUNT_NAME" default="@string/app_name" />
      <preference name="ACCOUNT_LABEL" default="@string/app_name" />
      <preference name="ACCOUNT_TYPE" default="$PACKAGE_NAME.account" />
      <preference name="CONTENT_AUTHORITY" default="$PACKAGE_NAME" />

      <framework src="com.google.android.gms:play-services-location:$GOOGLE_PLAY_SERVICES_VERSION" />
      <framework src="com.android.support:support-v4:$ANDROID_SUPPORT_LIBRARY_VERSION" />
      <framework src="com.android.support:appcompat-v7:$ANDROID_SUPPORT_LIBRARY_VERSION" />
      <framework src="src/android/dependencies.gradle" custom="true" type="gradleReference"/>

      <source-file src="src/android/com/globespinning/ionic/locationmanager/GSLocationManager.java"
                   target-dir="src/com/globespinning/ionic/locationmanager"></source-file>

      <config-file target="AndroidManifest.xml" parent="/manifest">
         <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
         <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
         <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
         <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
         <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
         <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
         <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
         <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
         <uses-permission android:name="android.permission.INTERNET" />
         <uses-permission android:name="android.permission.WAKE_LOCK" />
         <uses-permission android:name="android.hardware.location" />
      </config-file>

      <config-file target="res/xml/config.xml" parent="/*">
          <feature name="LocationManager">
              <param name="android-package" value="com.globespinning.ionic.locationmanager.GSLocationManager" />
          </feature>
      </config-file>
    </platform>
</plugin>