首页 > 解决方案 > 这个通知“标识符'gridOptions'指的是componentAngular的私有成员”是否告诉我我没有遵循编码最佳实践?

问题描述

我正在使用 ag-grid 作为构建网格的框架(显然)。
我遵循了一个简单的教程。所以这是到目前为止的代码,

排版.component.html 在此处输入图像描述

排版.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”指的是组件的私有成员

这是可以理解的。我正在关注官方教程。我想知道代码是否违反了任何编码规则或最佳实践?
谢谢你。

标签: htmlnode.jsangulartypescriptag-grid

解决方案


当您gridOptions在模板中使用时,不应将其声明为private变量。

对此的更多参考:Angular2 - 在模板中是否可以访问私有变量?

另一个线程仍在 GitHub 上打开,但您会对此有更多想法:访问私有属性时出现更好的离线编译器错误


推荐阅读