首页 > 解决方案 > VSCode 无法识别模板中的变量

问题描述

我正在学习名为 Learning AngularJS 2 的 Lynda 课程,并且我已经完成了让应用程序正常工作的任务。它可以正常编译并在浏览器中按预期工作。但是,Visual Studio Code在所有模板文件中都给了我问题警告:

未定义标识符“艺术家”。组件声明、模板变量声明和元素引用不包含这样的成员

艺术家详细信息.component.html

<div class="container mt-3">
    <div class="row justify-content-center">
  <div class="col-sm-4 col-lg-3">
    <img class="img-fluid rounded" src="assets/images/{{artist.shortname}}.jpg" alt="{{artist.name}} photo">
  </div>
  <div class="col-sm-8 col-lg-4">
    <h2 class="mt-3 mt-sm-0 mb-0">{{artist.name}}</h2>
    <h4 class="mt-0">{{artist.reknown}}</h4>
    <p>{{artist.bio}}</p>
  </div>
</div>

艺术家详细信息.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-artist-details',
  templateUrl: './artist-details.component.html',
  inputs: ['artist']
})
export class ArtistDetailsComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }    
}

我已经安装了Angular Essentials扩展。

标签: angularvisual-studio-codesyntax-highlighting

解决方案


好吧,我发现它似乎是一个 Visual Studio Code 错误。写作artist['name']而不是artist.name使警告消失。


推荐阅读