簡體   English   中英

Typescript使用Angular 2組件拋出聲明預期錯誤

[英]Typescript throws Declaration Expected error with Angular 2 component

我使用typescript構建一個角度2應用程序,我嘗試創建新的組件調用sidekik.component.ts並導入到這樣的app.component.ts。

app.component.ts

import {Component} from 'angular2/core';
import {SideKikComponent} from './classes/sidekik.component';

interface Hero {
   id: number;
   name: string;
}

@Component({
  selector: 'my-app',
  directives:[
    SideKikComponent,
  ],
  templateUrl:'app/views/heros.html',
  styleUrls: ['app/css/site.css']
})

export class AppComponent { 
  public title = 'Tour of Heroes';
  public heroes =HEROS;
  public selectedHero: Hero;

  onSelect(hero: Hero) { 
    this.selectedHero = hero; 
  }
}

var HEROS: Hero[] =[
  {"id":1,"name":"SuperMan"},
  {"id":2,"name":"Captain America"},
  {"id":3,"name":"Thor"},
  {"id":4,"name":"Iorn Man"},
  {"id":5,"name":"Ant Man"}
];

sidekik.component.ts

import {Component, View} from 'angular2/core';

@Component({
  selector:'sidekik',
  events:['hit'],
  properties:['define'],
  template: `
    <sidekik (click) = "hit(define)"></sidekik>
`,

});

export class SideKikComponent{
    hit(define:string){
    console.log(define);
  }
}

然后我運行npm start它顯示

rumes@rumes-HP-ProBook-4530s:/var/www/html/angular2ts$ tsc --version
message TS6029: Version 1.7.5
rumes@rumes-HP-ProBook-4530s:/var/www/html/angular2ts$ npm start

> angular2ts@1.0.0 start /var/www/html/angular2ts
> concurrent "npm run tsc:w" "npm run lite" 

[0] 
[0] > angular2ts@1.0.0 tsc:w /var/www/html/angular2ts
[0] > tsc -w
[0] 
[1] 
[1] > angular2ts@1.0.0 lite /var/www/html/angular2ts
[1] > lite-server
[1] 
[1] [BS] Access URLs:
[1]  ------------------------------------
[1]        Local: http://localhost:3000
[1]     External: http://192.168.1.7:3000
[1]  ------------------------------------
[1]           UI: http://localhost:3001
[1]  UI External: http://192.168.1.7:3001
[1]  ------------------------------------
[1] [BS] Serving files from: ./
[1] [BS] Watching files...
[0] app/classes/sidekik.component.ts(11,3): error TS1146: Declaration  expected.
[1] 15.12.17 16:04:28 304 GET /./index.html (Unknown - 29ms)
[0] 4:04:28 PM - Compilation complete. Watching for file changes.
[1] 15.12.17 16:04:28 304 GET /node_modules/angular2/bundles/angular2-polyfills.js (Unknown - 310ms)
[1] 15.12.17 16:04:28 304 GET /node_modules/systemjs/dist/system.src.js (Unknown - 310ms)
[1] 15.12.17 16:04:28 304 GET /node_modules/rxjs/bundles/Rx.js (Unknown - 310ms)
[1] 15.12.17 16:04:28 304 GET /node_modules/angular2/bundles/angular2.dev.js (Unknown - 310ms)
[1] [BS] File changed: app/classes/sidekik.component.js
[1] [BS] File changed: app/app.component.js
[1] [BS] File changed: app/boot.js
[1] 15.12.17 16:04:29 200 GET /app/boot.js (Unknown - 40ms)
[1] 15.12.17 16:04:30 200 GET /app/app.component.js (Unknown - 92ms)
[1] 15.12.17 16:04:30 200 GET /app/classes/sidekik.component.js (Unknown - 75ms)
[1] 15.12.17 16:04:31 304 GET /app/views/heros.html (Unknown - 227ms)
[1] 15.12.17 16:04:31 404 GET /favicon.ico (Unknown - 229ms)

誰能幫我這個?。

我遇到了同樣的問題(在編譯時Typescript Declaration expected發出的Typescript Declaration expected ),雖然它也表現為Angular在瀏覽器中拋出的異常:

No Directive annotation found on [Errant Module]

這對我有用:

刪除@Component();末尾的分號@Component(); (sidekik.component.ts)

一個簡單的解釋是裝飾器是返回函數的表達式 也就是說,裝飾者不是聲明 語句告訴編譯器要做的事情,並且需要停止分號(或新行)。 表達式只返回值。

所以裝飾者不能以分號結尾,因為它們不是語句,因為如果可以的話,這將使編寫JS引擎的人的生活更加艱難(這是猜測)。

您可以在此處了解有關裝飾器的更多信息

問題是將@Component()與SideKikComponent類分開的分號。 @Component是附加到其后面的類的注釋。 所以一堂課必須遵循它。 如果刪除半冒號以及導出的類,以便@Component()位於文件的底部,則會得到完全相同的錯誤,因為@Component需要一個類。

“@Component是一個注釋,它告訴Angular,注釋附加到的類是一個組件。” - http://blog.thoughtram.io/angular/2015/05/03/the-difference-between-annotations-and-decorators.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM