【问题标题】:Module parse failed:You may need an appropriate loader to handle this file type模块解析失败:您可能需要适当的加载程序来处理此文件类型
【发布时间】:2018-02-27 12:07:59
【问题描述】:

如果问题很简单,请见谅。我刚刚开始学习 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>

这是错误: 请您就如何解决这个问题提供一些帮助吗?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

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

    模板是 ECMAScript 2015 反引号中的多行字符串 (). The backtick ()——与单个字符不同 引号 (') — 允许您将一个字符串组合成多行,这 使 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 请务必接受答案,以便将来的读者作为经过验证的答案来帮助
    猜你喜欢
    • 2019-10-11
    • 2017-06-20
    • 1970-01-01
    • 2017-03-25
    • 2020-07-30
    • 2017-10-22
    • 1970-01-01
    • 2019-01-28
    • 2018-07-12
    相关资源
    最近更新 更多