Angular Component Class member attribute default access control

In Angular, if no access modifier (such as private, public, or protected) is explicitly used on a member of a Component Class, the default access control is public.

The Public access modifier means that members can be accessed from anywhere, including inside the class, outside the class, and derived classes. TypeScript (the base language used by Angular) marks members as public by default when no access modifier is explicitly specified.

Here is some important information about the default access controls:

  1. Default access modifier: If class members (properties or methods) declared in Angular do not specify an access modifier, they will be set to public by default.

For example, the following code snippet shows an example of an Angular Component Class without explicitly specifying access modifiers:

@Component({
    
    
  selector: 'app-example',
  template: '<p>{
    
    {message}}</p>'
})
export class ExampleComponent {
    
    
  message: string; // 默认访问修饰符是public
  // ...
}

In the above example, messagethe member is defaulted to public access modifier, which means it can be accessed from both inside and outside the class.

  1. Access outside the class: If a member has the public access modifier, it can be accessed on the instance of the class, and can also be accessed outside the class through the instance of the class.
const example = new ExampleComponent();
example.message = 'Hello'; // 类外部可以访问public成员

In the above example, through the created ExampleComponentinstance example, we can directly access and set messagethe value of the member.

  1. Internal class access: Since the default access control is public, within the class, all members of the class can be directly accessed, including those members that do not explicitly specify access modifiers.
export class ExampleComponent {
    
    
  message: string; // 默认访问修饰符是public

  constructor() {
    
    
    this.message = 'Hello'; // 类内部可以访问public成员
  }
}

In the above example, messagethe member can directly access and set its value in the constructor of the class.

In Angular, if you do not specify an access modifier (public, private, or protected) for a class member (such as a property or method), its default modifier is public. This is the rule of TypeScript, and Angular follows it.

public is the most basic access modifier in TypeScript, it means that the class members are public and can be accessed from anywhere. In practical applications, you may see that many class members do not have explicit access modifiers, which means that they are public and can be accessed inside the class, subclasses, and outside the class.

However, while the default access control is public, when writing code, it's a best practice to always specify access modifiers explicitly. This makes your intent clear and makes it easier for other developers to understand your code.

In Angular, the use of access modifiers has further implications. For example, when you add the private or protected modifier before a class member, it means that the class member can only be accessed in the current class or subclasses, but not outside the class. This helps encapsulate and hide internal implementation details, improving code maintainability.

In addition, Angular's templates do not follow TypeScript's access control rules. Even if a class member is marked as private or protected, it can still be accessed in the template of that class. However, this does not mean that developers should access private or protected members in templates. This is still a violation of encapsulation and should be avoided as much as possible.

In Angular's component class, there are usually some special class members, such as life cycle hook methods (such as ngOnInit, ngOnChanges, etc.). These methods usually have no access modifiers because they are called by the Angular framework and should not be directly accessed by external code.

Summarize

In Angular, if a member in a Component Class is not modified with private, the default access control is public, that is, the member can be accessed anywhere. However, for code clarity and maintainability, you should always specify access modifiers for class members, clearly indicating their access rights.

At the same time, it should be noted that Angular's templates do not obey TypeScript's access control rules, but you should still avoid accessing private or protected members in templates.

Guess you like

Origin blog.csdn.net/i042416/article/details/131649628