属性'x'是私有的,只能在'y'类中访问 [英] Property 'x' is private and only accessible within class 'y'

查看:725
本文介绍了属性'x'是私有的,只能在'y'类中访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

 从'@ angular / core'导入{Component};来自'ionic-angular'的
import {NavController,Loading,Alert};

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

导出类SearchPage {
构造函数(....)
{
//代码在这里
}

findItems()
{
let loading = Loading.create({
content:查找项目......
});

this.nav.present(loading);
//这里的其他东西
}

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

 错误TS2341:属性'create'是私有的,只能在类'Loading 


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

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

在这种情况下,在我的终端中出现以下消息:错误TS2341:属性' create'是私有的,只能在课堂'Alert'中访问。



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

解决方案

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



这是因为 create private 函数正在加载,因此无法在加载类。



代码示例来自Ionic的文档显示了一个 LoadingController 类,用于使用所需选项实例化Loading对象。我会从那里开始。

 从'ionic-angular'导入{LoadingController}; 

// ...

构造函数(private loadingController:LoadingController){

}

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

loading.present();
//这里的其他东西
}


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

解决方案

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
}

这篇关于属性'x'是私有的,只能在'y'类中访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆