【问题标题】:Angular2 Error: Return type of public method from exported class has or is using private nameAngular2 错误:从导出类返回的公共方法的类型已经或正在使用私有名称
【发布时间】:2016-12-22 06:16:05
【问题描述】:

在最初使用非 CLI 版本的 Angular 2 构建应用程序的工作版本之后,我正在使用 Angular-CLI 构建一个 Angular 2 应用程序。令我惊讶的是,一些在我的代码中不是问题的代码非 CLI 应用程序版本一直是我的 Angular-CLI 版本的问题。综上所述,我已经解决了所有问题,除了我遇到的最后一个错误。

这是我收到的错误消息:

未捕获的错误:模块构建失败:错误: /Users/fdr/Documents/rds/rds/cli-rds/src/app/ui/generate-field.component.ts (340,48):从导出类返回的公共方法的类型有或者是 使用私有名称“提示”。)

这是导致错误的问题文件:

import { Component, Input, Output, EventEmitter, OnInit, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { EventHandler } from '../app.event-handler';
import  '../app.utils';

@Component({
    selector: 'app-generate-field',
    templateUrl: 'app/ui/generate-field.component.html',
    styleUrls: ['app/ui/generate-field.component.css']

})
export class GenerateField extends EventHandler
{

    public get hasFocus(): boolean
    {
        return this._hasFocus;
    }

    @Input() delay: number = 300;


    @ViewChild('inputField') private inputField: ElementRef;


    @ViewChild('suggestionField') private suggestionField: ElementRef;


    @Input() public value: string;


    @Output() private valueChange: EventEmitter<string> = new EventEmitter<string>();


    @Output() public keyup: EventEmitter<KeyboardEvent> = new EventEmitter<KeyboardEvent>();

    @Output() public focus = new EventEmitter<KeyboardEvent>();

    @Output() public blur = new EventEmitter<KeyboardEvent>();

    private inlineSuggestion: string;

    private suggestions: ISuggestion[];

    @Input() public options: string[];

    @Output() private optionsChange: EventEmitter<string[]> = new EventEmitter<string[]>();

    private isDirty: boolean = false;

    private _hasFocus: boolean = false;


    constructor(myElement: ElementRef)
    {
        super();


        this.defineObservableProperty('value');
        this.defineObservableProperty('isDirty');
        this.defineObservableProperty('suggestions');
        this.defineObservableProperty('options');

        this.addPropertyListener('isDirty', function ()
        {
            if (this.isDirty == false)return;

            var delay = this.delay ? this.delay : 500;

            var self = this;

            setTimeout(function ()
            {
                self.updateSuggestions();

                this.isDirty = false;

            }.bind(this), delay);

        }.bind(this));

        this.addPropertyListener('value', (): void=>
        {
            this.valueChange.emit(this.value);

            this.isDirty = true;
        });

        this.addPropertyListener('suggestions', (): void=>
        {
            this.updateInlineSuggestion();
        });

        this.addPropertyListener('options', ()=>
        {
            this.optionsChange.emit(this.options);
        });

    }

    //--------------------------------------------------------
    // Functions
    //--------------------------------------------------------

    /**
     * Evaluates value and updates the list of suggestions
     */
    public updateSuggestions(): void
    {
        // Update suggestions
        this.suggestions = this.generateSuggestions(this.value);
    }

    /***
     * Updates the inline suggestion that appears on the text field
     */
    private updateInlineSuggestion(): void
    {
        // Clear inline if there are no suggestions
        if (this.suggestions.length == 0)
        {
            this.inlineSuggestion = '';
            return;
        }

        // Show first option inline
        this.inlineSuggestion = this.suggestions[0].value;

        var x = this.inputField.nativeElement.selectionStart;
        var y = this.inputField.nativeElement.selectionEnd;

        this.suggestionField.nativeElement.selectionStart = x;
        this.suggestionField.nativeElement.selectionEnd = y;

        this.inputField.nativeElement.selectionStart = x;
        this.inputField.nativeElement.selectionEnd = y;

        this.suggestionField.nativeElement.scrollLeft = x;

    }

    private onFocus(): void
    {
        this._hasFocus = true;

        // Forward event
        this.focus.emit();
    }

    private onBlur(): void
    {
        this._hasFocus = false;

        // Forward event
        this.blur.emit();
    }


interface ISuggestion
{

    word: string;

    match: string;

    value: string;
}

【问题讨论】:

  • 你检查过ISuggestion 是否是私人的吗?如果是,您为什么将其设为私有?
  • 这部分代码不是我写的——另一个开发者写了。我现在正在查看代码。
  • 我确实在文件中找到了这个参考:私人建议:ISuggestion[];
  • 将其更改为“公共建议:ISuggestion[];”没有解决问题。而且我很好奇 - 如果这是问题代码 - 为什么它在 Angular-CLI 版本中是一个问题,而不是标准 Angular 2 版本?只是试图理解。另外,如果不只是将其设置为“公共”,您将如何建议我编辑此代码?谢谢。
  • 错误不是关于suggestions 是私有的,而是关于ISuggestion 是私有的。

标签: javascript angular typescript angular-cli


【解决方案1】:

尝试在代码的最后部分添加“export interface ISuggestion”,以便将 ISuggestion 也导出。

【讨论】:

  • 起初我以为这并没有解决问题,但实际上它确实解决了。所以谢谢!
【解决方案2】:

尝试在您的方法之后添加 : any。我遇到了同样的问题,添加任何后就解决了。

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 2017-02-13
    • 2014-01-03
    • 2014-11-20
    • 1970-01-01
    • 2017-02-23
    • 2013-04-05
    • 2011-07-21
    • 2018-11-09
    相关资源
    最近更新 更多