【问题标题】:Angular2 error TS1146: Declaration expectedAngular2 错误 TS1146:预期声明
【发布时间】:2017-09-22 22:30:26
【问题描述】:

这里是 Angular2 新手。

我正在使用来自 Angular.io 的种子文件,但是当我运行“npm start”时,我得到一个 tsc 编译器错误 -

tsc -p src/

src/app/app.module.ts(11,3):错误 TS1146:需要声明。

谁能告诉我我错过了什么。我只有一个组件“AppComponent”。

app.module.ts:

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

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

app.component.ts

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

@Component({
  selector: 'my-app',
  template: `
    <h1>Hello</h1>
  `,
})

export class AppComponent  {

}

和 index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <base href="/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>

    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading AppComponent content here ...</my-app>
  </body>
</html>

【问题讨论】:

  • 你能把template: ' &lt;h1&gt;Hello&lt;/h1&gt; ',的逗号去掉吗?

标签: angular compiler-errors


【解决方案1】:

我今天遇到了这个问题,因为我在我的 angualr 6 应用程序中犯了一个愚蠢的错误。我来自 C# 世界,我总是在每行之后使用分号 (;)。我在我的组件中做了同样的事情。

@Component({
  selector: 'app-movies',
  templateUrl: './movies.component.html',
  styleUrls: ['./movies.component.css']
});

稍后,这个post 驱使我进行修复,即我需要删除@Component 之后的分号 (;)。

@Component({
  selector: 'app-movies',
  templateUrl: './movies.component.html',
  styleUrls: ['./movies.component.css']
})

但我不确定,为什么我们会在这种情况下收到此错误,我希望有人能尽快解决此问题。

【讨论】:

    【解决方案2】:

    对我来说一切都很好,除了模板后面可能有一个额外的逗号......

    app.component.ts

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
        <h1>Hello</h1>
      `
    })
    
    export class AppComponent  {
    }
    

    【讨论】:

    • 谢谢。是的,逗号是问题所在。需要更加小心。
    猜你喜欢
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多