简体   繁体   English

这个通知“标识符'gridOptions'是指componentAngular的私有成员”告诉我,我没有遵循编码最佳实践?

[英]Is this notice “Identifier 'gridOptions' refers to a private member of the componentAngular” telling me that I am not following coding best practices?

I am using ag-grid as a framework to build my grids(OBVIOUSLY). 我使用ag-grid作为构建我的网格的框架(显然)。
I have followed a simple tutorial. 我遵循了一个简单的教程。 So here's the code so far, 所以这是迄今为止的代码,

typography.component.html typography.component.html 在此输入图像描述

typography.component.ts typography.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}
        ]
    }
}  

This works perfecly. 这完美地工作。 However, as you may have noticed in the html file: 但是,正如您在html文件中注意到的那样:

gridOptions gridOptions

is underlied in red. 用红色表示。 And this is why: 这就是为什么:

Identifier 'gridOptions' refers to a private member of the component 标识符'gridOptions'指的是组件的私有成员

That is understandable. 这是可以理解的。 I am following the official tutorial. 我正在关注官方教程。 And I am wondering whether the code breaks any coding rules or best practices? 我想知道代码是否破坏了任何编码规则或最佳实践?
Thank you. 谢谢。

As you are using gridOptions in your template, it should not be declared as private variable. 当您在模板中使用gridOptions时,不应将其声明为private变量。

More reference on this: Angular2 - should private variables be accessible in the template? 更多参考: Angular2 - 应该在模板中访问私有变量吗?

Another thread is still open on GitHub but you will get some more ideas regarding this: Better offline compiler error when accessing private properties 另一个线程仍然在GitHub上打开,但您将获得更多关于此的想法: 访问私有属性时更好的脱机编译器错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM