html - 这个通知“标识符'gridOptions'指的是componentAngular的私有成员”是否告诉我我没有遵循编码最佳实践?
问题描述
我正在使用 ag-grid 作为构建网格的框架(显然)。
我遵循了一个简单的教程。所以这是到目前为止的代码,
排版.component.ts
import {Component} from "@angular/core";
import {GridOptions} from "ag-grid-community";
@Component({
selector: 'typography-cmp',
moduleId: module.id,
templateUrl: 'typography.component.html'
})
export class TypographyComponent {
private gridOptions: GridOptions;
constructor() {
this.gridOptions = <GridOptions>{};
this.gridOptions.columnDefs = [
{
headerName: "ID",
field: "id",
width: 100
},
{
headerName: "Value",
field: "value",
width: 100
},
];
this.gridOptions.rowData = [
{id: 5, value: 10},
{id: 10, value: 15},
{id: 15, value: 20}
]
}
}
这非常有效。但是,您可能已经在 html 文件中注意到:
网格选项
是红色的。这就是为什么:
标识符“gridOptions”指的是组件的私有成员
这是可以理解的。我正在关注官方教程。我想知道代码是否违反了任何编码规则或最佳实践?
谢谢你。
解决方案
当您gridOptions
在模板中使用时,不应将其声明为private
变量。
对此的更多参考:Angular2 - 在模板中是否可以访问私有变量?
另一个线程仍在 GitHub 上打开,但您会对此有更多想法:访问私有属性时出现更好的离线编译器错误
推荐阅读
- python - 根据标准创建截止日期
- android - 新材质组件中的按钮无法使用 android:background
- c# - 注册一个 Autofac 模块并返回 IServiceCollection
- rust - 在遍历 Vec 的元素时使用 powi
- python-3.x - asyncio 在监听 http 请求时不起作用
- android - RecyclerView 无法应用于 FlexboxLayoutManager
- linux - Vimwiki C-Enter 或 S-Enter 在 Gnome 终端中不起作用
- php - 如何让我的 WordPress 导航链接显示出来?
- angular - Angular 和 AngularFire | 传递变量 - 例子/好的参考?
- python - 如何将定义的函数应用于 Python 中的数组?