当前位置:  开发笔记 > 编程语言 > 正文

Typescript使用Angular 2组件抛出声明预期错误

如何解决《Typescript使用Angular2组件抛出声明预期错误》经验,为你挑选了1个好方法。

我使用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: `
    
`,

});

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)

谁能帮我这个?.



1> Bryce Johnso..:

我有同样的问题(Typescript Declaration expected在ts编译时发出),虽然它也表现为Angular在浏览器中抛出的异常:

No Directive annotation found on [Errant Module]

这对我有用:

删除@Component();(sidekik.component.ts)末尾的分号

一个简单的解释是装饰器是返回函数的表达式.也就是说,装饰者不是声明.语句告诉编译器要做的事情,并且需要停止分号(或新行).表达式只返回值.

所以装饰者不能以分号结尾,因为它们不是语句,因为如果可以的话,这将使编写JS引擎的人的生活更加艰难(这是猜测).

您可以了解更多关于装饰在这里.

推荐阅读
赛亚兔备_393
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有