模块解析失败:您可能需要一个适当的加载器来处理此文件类型。

3

如果问题很简单请原谅我,我刚开始学习Angular。我正在尝试我的第一个应用程序,但它无法编译,因为它无法识别我的代码的一部分。请问有人能给我一些提示吗?

app.component.html文件:

<!--The whole content below can be removed with the new code.-->
<div style="text-align:center">
  <h1>
    Welcome to {{pageTitle}}!!
  </h1>
  ... Starter Files ...
</div>

app.component.ts文件

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

@Component({
  selector: 'pm-root',
  template: <div><h1>{{pageTitle}}</h1></div> // **this line of code created error**  
})
export class AppComponent {
  pageTitle: string = 'Angular: Getting Started';
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Acme Product Management</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <pm-root></pm-root>
</body>
</html>

这里出现了一个错误:enter image description here。请问是否能提供一些帮助来解决这个问题?

1个回答

3

在您的模板中没有反引号或引号:

模板是ECMAScript 2015反引号()内的多行字符串。反引号(),它不是单引号(')相同的字符,可以使你将一个字符串组合成多行,这可以让HTML更易于阅读。

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

    @Component({
      selector: 'pm-root',
      template: `<div><h1>{{pageTitle}}</h1></div>` // **added backticks**  
    })
    export class AppComponent {
      pageTitle: string = 'Angular: Getting Started';
    }

非常感谢您的快速帮助,Iingthe。现在正在编译。 - user1836957
@user1836957 请接受答案,这将有助于未来读者作为验证的答案。 - Hemadri Dasari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接