简体   繁体   English

私人成员可使用angular 2装饰器访问

[英]private members are accessible in angular 2 decorators

Consider this code: 考虑以下代码:

export class Hero {
    constructor(private id: number, private name: string) {}
}

@Component({
    selector: 'my-app',
    template: '<h1>{{title}}</h1><h2>{{hero.name}} details!</h2>'
})
export class AppComponent {
    private title = "Tour of Heroes";
    private hero: Hero = new Hero(1, "Windstorm");
}

In AppComponent 's template I wrote hero.name , however, this field is private according to the Hero class and should not be accessible. AppComponent的模板中,我编写了hero.name ,但是,根据Hero类,此字段是私有的,不应访问。 How this code compiles and works? 该代码如何编译和工作? am I missing something? 我错过了什么吗?

EDIT: Read the answers on why it happens, here's my way of handling this, it's not a fix but it keeps things more organized and safe , besides accessors are always good to use: 编辑:请阅读有关其发生原因的答案,这是我处理此问题的方法,它不是修复程序,但可以使事情更井井有条,更安全 ,此外访问器始终很容易使用:

export class Hero {
    constructor(private _id: number, private _name: string) { }

    get name(): string {
        return this._name;
    }

    get id(): number {
        return this._id;
    }
}

@Component({
    selector: 'my-app',
    template: '<h1>{{title}}</h1><h2>{{hero.name}} details!</h2>'
})
export class AppComponent {
    private title = "Tour of Heroes";
    private hero: Hero = new Hero(1, "Windstorm");
}

When hero.name will be executed in JS it should call the JS-compiled getter function you defined in your TS code, this should give some sort of control over the properties while maintaining TS code-style. hero.name将在JS中执行时,应调用您在TS代码中定义的JS编译的getter函数,这应在保持TS代码样式的同时对属性进行某种控制。

In JavaScript there is no such thing as private variables. 在JavaScript中,没有私有变量之类的东西。 Keywords like private are just used by the TypeScript transpiler to enforce constraints before transpilation. TypeScript转译器仅使用诸如private类的关键字在转译前强制执行约束。 Once the code is transpiled into JavasScript, the name property is a visible member of the Hero class. 将代码转换为JavasScript之后, name属性是Hero类的可见成员。

The private keyword in typescript is just used for compile time checking and doesn't actually restrict access to anything at runtime. typescript中的private关键字仅用于编译时检查,实际上并不限制运行时对任何内容的访问。 The typescript compiler isn't checking your templates so it's not catching the issue. 打字稿编译器没有检查您的模板,因此没有发现问题。

I believe some IDE's (VS Code and WebStorm) are working on type checking your templates but for now you are on your own 我相信某些IDE(VS Code和WebStorm)正在对模板进行类型检查,但是目前您是一个人。

Angular2 states referring private variables within templates as a correct way. Angular2指出在模板中引用私有变量是正确的方法。 See cheat-sheet: https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child-setter . 请参阅备忘单: https : //angular.io/docs/ts/latest/cookbook/component-communication.html# !#parent-to-child- setter

So go ahead! 所以请继续! And enable encapsulation to your components not exposing every variables as public. 并启用对组件的封装,而不将每个变量都公开。

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

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