Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 error TS1146: Declaration expected

Angular2 Newbie here.

I'm using the seed files from Angular.io but when I run 'npm start' I get a tsc compiler error -

tsc -p src/

src/app/app.module.ts(11,3): error TS1146: Declaration expected.

Can someone tell me what I'm missing please. I have only one component '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  {

}

and 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>
like image 470
Brasso Avatar asked Apr 25 '17 18:04

Brasso


2 Answers

I was facing this issue today because of a silly mistake I had done in my angualr 6 application. I am from the C# world, and I always use semicolon (;) after every lines. I did the same here in my component.

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

Later, this post drives me to the fix, that I need to remove the semicolon (;) after the @Component.

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

But I am not sure, why we get this error for this scenario, I hope someone will fix this very soon.

like image 156
Sibeesh Venu Avatar answered Nov 09 '22 23:11

Sibeesh Venu


Everything looks good to me except for maybe an extra comma after your template...

app.component.ts

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

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

export class AppComponent  {
}
like image 37
birwin Avatar answered Nov 09 '22 23:11

birwin