Property 'x' is private and only accessible within class 'y'

人盡茶涼 提交于 2020-01-04 08:05:49

问题


I have this piece of code:

   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
    }

When I run ionic serve everything shows correctly but when I click a button which calls findItems() method I get this error:

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

An analogous errors appears if I do:

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

In this case in my terminal appears following message:Error TS2341: Property 'create' is private and only accessible within class 'Alert'.

I'm working with Ionic2 version 2.0.0-beta.36


回答1:


EDIT: This only applies to beta 11 and higher

This is because create is a private function of the class Loading, and therefore not callable outside of the Loading class.

The code example from Ionic's documentation shows a LoadingController class used to instantiate the Loading object with the desired options. I would start there.

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
}



回答2:


It looks like the syntax for Alerts has changed. Here's the new syntax:

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();
  }
}


来源:https://stackoverflow.com/questions/38837964/property-x-is-private-and-only-accessible-within-class-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!