【问题标题】:Property 'x' is private and only accessible within class 'y'属性“x”是私有的,只能在类“y”中访问
【发布时间】:2016-12-14 17:53:44
【问题描述】:

我有这段代码:

   import { Component } from '@angular/core';
    import { NavController, Loading, Alert } from 'ionic-angular';

    @Component({
      templateUrl: 'build/pages/search/search.html',
    })

    export class SearchPage {
    constructor (....)
    {
         // code here
    }

    findItems()
    {
            let loading = Loading.create({
                content: "Finding items..."
            });

            this.nav.present(loading);
            // other stuff here
    }

当我运行ionic serve 时,一切都显示正确,但是当我单击调用findItems() 方法的按钮时,我收到此错误:

Error TS2341: Property 'create' is private and only accessible within class 'Loading

如果我这样做会出现类似的错误:

let alert = Alert.create({
                    title: 'Hello!',
                });

在这种情况下,我的终端会出现以下消息:Error TS2341: Property 'create' is private and only accessible within class 'Alert'.

我正在使用 Ionic2 版本 2.0.0-beta.36

【问题讨论】:

  • 您使用的是Ionic beta 11 还是以前的版本?您可以通过查看您的 package.json 并找到类似 "ionic-angular": "^2.0.0-beta.11", 的内容来了解​​这一点
  • @sebaferreras 在 package.json 我有:“ionic-angular”:“2.0.0-beta.10”
  • 嗯,一切似乎都正常。你能看看this plunker吗?
  • @splunk 您可能希望更新您的帖子以反映您使用的是 beta 10。我的回答是基于您的问题中列出了 beta 36 的事实。

标签: javascript angular typescript ionic-framework ionic2


【解决方案1】:

编辑:这仅适用于 beta 11 及更高版本

这是因为createLoading 类的private 函数,因此不能在Loading 类之外调用。

Ionic 文档中的code example 显示了一个LoadingController 类,用于实例化具有所需选项的Loading 对象。我会从那里开始。

import { LoadingController }  from 'ionic-angular';

//...

constructor(private loadingController: LoadingController) {

}

findItems() {
  let loading = this.loadingController.create({
  content: "Finding items..."
  duration: 3000
  });

  loading.present();
  // other stuff here
}

【讨论】:

  • LoadingControllerIonic beta 11 中引入。我不确定来自 OP 的代码是否使用该版本或 Ionic beta 10
  • @sebaferreras 他在帖子中明确表示他正在使用 beta 36。根据他的评论,看起来他实际上正在使用 beta 10。
  • 最新版本是beta 11,所以可能是cli version 或其他。在其中一个 cmets 中,用户说ionic-angular 版本是beta 10
【解决方案2】:

Alerts 的语法似乎已更改。这是新的语法:

import { AlertController } from 'ionic-angular';

export class MyPage {
  constructor(private alertController: AlertController){
  }

  showAlert() {
    let alert = this.alertController.create({
      title: 'New Friend!',
      subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
      buttons: ['OK']
    });
    alert.present();
  }
}

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 2017-02-23
    • 2017-08-27
    • 2021-01-10
    • 2019-06-17
    • 2018-01-01
    • 2020-03-04
    相关资源
    最近更新 更多