mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
459b275a3a
The commit adds messaging to the control flow template diagnostic to direct developers to the new built-in control flow syntax in Angular. PR Close #52268
85 lines
1.9 KiB
Markdown
85 lines
1.9 KiB
Markdown
@name Missing control flow directive
|
|
|
|
@description
|
|
|
|
This diagnostics ensures that a standalone component which uses known control flow directives
|
|
(such as `*ngIf`, `*ngFor`, `*ngSwitch`) in a template, also imports those directives either
|
|
individually or by importing the `CommonModule`. Alternatively, use Angular's
|
|
built-in control flow.
|
|
|
|
<code-example format="typescript" language="typescript">
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
standalone: true,
|
|
// Template uses `*ngIf`, but no corresponding directive imported.
|
|
template: `<div *ngIf="visible">Hi</div>`,
|
|
// …
|
|
})
|
|
class MyComponent {}
|
|
|
|
</code-example>
|
|
|
|
## How to fix the problem
|
|
|
|
Use Angular's built-in control flow instead.
|
|
|
|
<code-example format="typescript" language="typescript">
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
@Component({
|
|
standalone: true,
|
|
template: `@if (visible) { <div>Hi</div> }`,
|
|
// …
|
|
})
|
|
class MyComponent {}
|
|
|
|
</code-example>
|
|
|
|
Or make sure that a corresponding control flow directive is imported.
|
|
|
|
A directive can be imported individually:
|
|
|
|
<code-example format="typescript" language="typescript">
|
|
|
|
import {Component} from '@angular/core';
|
|
import {NgIf} from '@angular/common';
|
|
|
|
@Component({
|
|
standalone: true,
|
|
imports: [NgIf],
|
|
template: `<div *ngIf="visible">Hi</div>`,
|
|
// …
|
|
})
|
|
class MyComponent {}
|
|
|
|
</code-example>
|
|
|
|
or you could import the entire `CommonModule`, which contains all control flow directives:
|
|
|
|
<code-example format="typescript" language="typescript">
|
|
|
|
import {Component} from '@angular/core';
|
|
import {CommonModule} from '@angular/common';
|
|
|
|
@Component({
|
|
standalone: true,
|
|
imports: [CommonModule],
|
|
template: `<div *ngIf="visible">Hi</div>`,
|
|
// …
|
|
})
|
|
class MyComponent {}
|
|
|
|
</code-example>
|
|
|
|
|
|
<!-- links -->
|
|
|
|
<!-- external links -->
|
|
|
|
<!-- end links -->
|
|
|
|
@reviewed 2022-02-28
|