【问题标题】:Ionic3: Cannot declare InAppBrowserIonic3:无法声明 InAppBrowser
【发布时间】:2019-05-28 14:14:18
【问题描述】:

我想使用 InAppBrowser 打开所有 target blank 链接。我遵循文档,当我在构造函数上声明插件时总是出错:

无法解析 MyApp 的所有参数:(?, ?, ?)。

当我将private iab: InAppBrowser 放在构造函数上时,这个错误出现在我面前。

我的代码:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { InAppBrowser } from '@ionic-native/in-app-browser';
import { Platform } from 'ionic-angular';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})


export class HomePage {

 constructor(public navCtrl: NavController, public plt: Platform, private iab: InAppBrowser) {

    this.plt.ready().then((readySource) => {
        console.log("Ready!");
        window.open = this.iab.open;
    });
  }

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { InAppBrowser } from '@ionic-native/in-app-browser';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    InAppBrowser,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

有人知道它是什么吗?

谢谢!

【问题讨论】:

    标签: ionic-framework ionic3 inappbrowser


    【解决方案1】:

    您的代码看起来不错,所以问题似乎是因为您的项目使用的 @ionic-native/core 版本。

    如您所见 in the docs,Ionic 团队已更新 Ionic Native 命令以避免此错误:

    安装

    要将 Ionic Native 添加到您的应用程序中,请运行以下命令来安装核心包: npm install @ionic-native/core@4 --save

    还有……

    用法

    安装所需的插件 为您要添加的每个插件安装 Ionic Native 包。 例如,如果要安装相机插件,则需要运行以下命令:

    npm install @ionic-native/camera@4 --save

    然后使用 Cordova 或 Ionic CLI 安装插件。例如:

    ionic cordova plugin add cordova-plugin-camera

    注意两个命令中的 @4。这允许您安装正确版本的 Ionic Native 依赖项,即使您使用的是新的 CLI。


    TLDR; 因此,如果您使用 @4 安装插件,您可以像这样导入它:import { InAppBrowser } from '@ionic-native/in-app-browser';

    如果没有,您可能使用的是较新版本的 Ionic Native,因此您需要像这样导入它:import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'

    【讨论】:

    • @FernandoAureliano 能否将app.module.ts 文件的内容粘贴到您的问题中?
    • 我把它放在 app.module.ts 上,但我得到了同样的错误:无法解析 MyApp 的所有参数:(?, ?, ?)。 :(
    • 您的代码看起来不错...也许这是一个愚蠢的问题,但您是否已经尝试过停止应用程序并再次运行ionic serve
    • 我现在做了,我得到:浏览...' 不能分配给“NgModule”类型的参数。属性“提供者”的类型 [app-scripts] 不兼容。类型 '(typeof SplashScreen | InAppBrowserOriginal | { 提供:typeof ErrorHandler; useClass: [app-scripts] typeof Io...' 不可分配给类型 'Provider[]'。类型 'typeof SplashScreen | InAppBrowserOriginal | {
    • @FernandoAureliano 我已经更新了答案,包括有关问题原因的更多详细信息:)
    猜你喜欢
    • 2019-11-05
    • 2018-03-17
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2011-12-26
    • 2013-04-14
    相关资源
    最近更新 更多