使用带下划线前缀的Angular API方法是否安全? [英] Is it safe to use Angular API methods with underscore prefix?

查看:124
本文介绍了使用带下划线前缀的Angular API方法是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角材料 paginator 有一个以_开头的方法.

Angular material paginator has a method starting with _.

_changePageSize(pageSize: number) {
    // Current page needs to be updated to reflect the new page size. Navigate to the page
    // containing the previous page's first item.
    const startIndex = this.pageIndex * this.pageSize;
    const previousPageIndex = this.pageIndex;

    this.pageIndex = Math.floor(startIndex / pageSize) || 0;
    this.pageSize = pageSize;
    this._emitPageEvent(previousPageIndex);
  }

Paginator API 文档.但是,它在诸如之类的答案中被引用.

This method is not listed in the Paginator API documentation. However it's refered in answers like this.

在同一分页器组件中,一种方法定义为私有,并且在IDE中不可用.

In the same paginator component, a method defined as private and not available in IDE.

private _updateDisplayedPageSizeOptions() {
    if (!this._initialized) { return; }

    // If no page size is provided, use the first page size option or the default page size.
    if (!this.pageSize) {
      this._pageSize = this.pageSizeOptions.length != 0 ?
          this.pageSizeOptions[0] :
          DEFAULT_PAGE_SIZE;
    }

在AOT中,编译成功后会出错.

In AOT it gives error after successful compilation.

i「wdm」:已成功编译.

i 「wdm」: Compiled successfully.

ERROR in src/app/models/models.component.ts(153,22):

错误TS2341:属性"_updateDisplayedPageSizeOptions"是私有的 并且只能在"MatPaginator"类中访问.

error TS2341: Property '_updateDisplayedPageSizeOptions' is private and only accessible within class 'MatPaginator'.

我理解_按照惯例是指Java私有的,没有直接私有方法的概念.只需了解更多信息:

I understand _ by conventions refers private and no direct private method concept in Javascript. Just need more info on:

  • 是不应该依赖的内部方法吗?
  • 以角(或角材料)为例,以_开头的方法与常规方法有什么区别?
  • 如果它是changePageSize不是私有方法,为什么后缀_?如果是私有方法,为什么不将其标记为私有?这是矛盾之处还是我在这里遗漏了什么?
  • Is that an internal method that should not be relied on?
  • In angular (or in angular-material) what is the difference between a method that starts with _ and regular method?
  • If it's changePageSize is not a private method why _ suffix? If it's private method, why it's not marked as private? Is this inconsistency or something I'm missing here?

推荐答案

如果您的问题是:我应该在我的代码中做同样的事情吗?"然后不,使用private changePageSize(){...}-如果您担心安全性,请TypeScript帮助您,这就是它的目的.

If your question is: "Should I do the same in my code?" Then no, use private changePageSize(){...} - if you're worried about safety, let TypeScript help you, that's its purpose.

如果您想了解约定,那么不建议使用此函数,因为API无法保证以相同的方式工作,甚至在将来的更新中都存在.

If you're wondering about convention, then it's not advised to use this function as the API makes no guarantees that will will work the same way, or even exist in future updates.

这篇关于使用带下划线前缀的Angular API方法是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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