Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

huangapple go评论133阅读模式
英文:

Angular ngFor IntelliSense Shows 'any' Instead of the Type in visual studio code

问题

当在Angular模板中使用ngFor循环遍历一个Person数组时,变量person的类型显示为any而不是其实际类型。如何确保在循环内部对person进行正确的类型识别?

代码:

sample.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. class Person {
  3. name: string;
  4. age: number;
  5. constructor(name: string, age: number) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. }
  10. @Component({
  11. selector: 'app-sample',
  12. templateUrl: './sample.component.html',
  13. styleUrls: ['./sample.component.scss']
  14. })
  15. export class SampleComponent implements OnInit {
  16. personArray: Person[];
  17. constructor() { }
  18. ngOnInit() {
  19. const person1 = new Person("Alice", 30);
  20. const person2 = new Person("Bob", 25);
  21. this.personArray = [person1, person2];
  22. }
  23. }

sample.component.html

  1. <div *ngFor="let person of personArray">
  2. {{person.name}}
  3. </div>
英文:

Question:

When using ngFor in my Angular template to loop through a Person array, the type of the variable person is displayed as any instead of its actual type. How can I ensure proper type recognition for person within the loop

Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

Code:

sample.component.ts

  1. import { Component, OnInit } from &#39;@angular/core&#39;;
  2. class Person {
  3. name: string;
  4. age: number;
  5. constructor(name: string, age: number) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. }
  10. @Component({
  11. selector: &#39;app-sample&#39;,
  12. templateUrl: &#39;./sample.component.html&#39;,
  13. styleUrls: [&#39;./sample.component.scss&#39;]
  14. })
  15. export class SampleComponent implements OnInit {
  16. personArray: Person[];
  17. constructor() { }
  18. ngOnInit() {
  19. const person1 = new Person(&quot;Alice&quot;, 30);
  20. const person2 = new Person(&quot;Bob&quot;, 25);
  21. this.personArray = [person1, person2];
  22. }
  23. }

sample.component.html

  1. &lt;div *ngFor=&quot;let person of personArray&quot;&gt;
  2. {{person.name}}
  3. &lt;/div&gt;

答案1

得分: 1

希望这条消息能够找到你。为了启用类型检查,你可以在你的 tsconfig.json 文件中添加 strictTemplates 选项,像这样:

  1. "angularCompilerOptions": {
  2. "fullTemplateTypeCheck": true,
  3. "strictInjectionParameters": true,
  4. "strictTemplates": true
  5. }

更多信息请查看这里:https://angular.io/guide/template-typecheck

以及这里的配置信息:https://angular.io/guide/typescript-configuration

Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

英文:

hope this message find you well.

To enable the type checking, you can add the strictTemplates option in your tsconfig.json like this:

  1. &quot;angularCompilerOptions&quot;: {
  2. &quot;fullTemplateTypeCheck&quot;: true,
  3. &quot;strictInjectionParameters&quot;: true,
  4. &quot;strictTemplates&quot;: true
  5. }

for more information please check here: https://angular.io/guide/template-typecheck

and the configurations here: https://angular.io/guide/typescript-configuration

Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

Angular ngFor智能感知在Visual Studio Code中显示’any’而不是类型

huangapple
  • 本文由 发表于 2023年8月9日 08:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定