有人可以解释Angular 2中组件中私有属性和非私有属性之间的区别吗?像private_text和other_text在这个例子:
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'my-component',
template: '<div>{{private_text}}</div><div>{{other_text}}</div>'
})
export class MyComponent {
private private_text:string;
other_text:string;
constructor() {
this.private_text = "hello";
this.other_text = "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
ian*_*yer 13
上市:
打字稿成员默认是公共的.
因此,如果我有一个类myClass与该方法public myMethod() {}或只是myMethod(){}然后将我的类导入另一个文件.我现在在另一个类的构造函数中定义constructor(my_class: myClass) {}.这让我可以this.my_class.myMethod()在我的其他课程中随时随地打电话.如果它是私人的.这不行.
私人的:
"当一个成员被标记为私有时,无法从其包含的类外部访问它"
真的很困惑,为什么没有人引用这个.我认为以下链接确实会有所帮助.
https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers
Gün*_*uer 10
private并且public只能由静态分析TypeScript代码的工具使用.
在运行时私有和公共是没有意义的,因为JS不知道它们.
TypeScript工具也不检查模板字符串是否存在可能违反TypeScript规则的绑定表达式.这就是模板无论属性是私有还是公共都无关紧要的原因.
据我所知,计划改进模板分析功能以识别所有类型的错误.但这只是未来.
| 归档时间: |
|
| 查看次数: |
13573 次 |
| 最近记录: |