Angular 2 - 无法在组件声明中导入另一个组件

时间:2017-02-26 12:32:05

标签: javascript angularjs angular typescript

我是Angular 2的新手,正在尝试使用它,这是我遇到的问题

与组件代码

相同

like.component.ts

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

@Component({
  selector: 'like',
  template: `
  <span (click) ="onClick()" class="glyphicon glyphicon-heart" [class.liked]="isLike" ></span>
  <span>{{likeCount+incrmentCOunt}}</span>

  `,
  styles:[`
        .glyphicon-heart{
          color:#ccc;
        }

        .liked{
          color:deeppink;
        }
  `]

})
export class LikeComponent  { 
  @Input("like") isLike = false;
  @Input() likeCount:number;
  incrmentCOunt=0;
  onClick(){
   if(this.isLike){
     this.isLike=false;
this.incrmentCOunt=0;
   }else{
     this.isLike=true;
     this.incrmentCOunt=1;
        }
  }

}

在实现嵌套组件的同时,我将所有组件导入到ngModule装饰器中,并将其添加为此类声明

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TweetComponent } from './tweet/tweet.component';
import { AppComponent }  from './app.component';
import { LikeComponent } from './like/like.component';
@NgModule({
    imports:      [ BrowserModule ],
    declarations: [ AppComponent ,TweetComponent,LikeComponent],
    bootstrap:    [ AppComponent ]
})
export class AppModule { }

这工作得很好,现在我决定从ngModule中删除LikeComponent,并尝试在TweetComponent中导入它,

tweet.component.ts

import { Component,Input } from '@angular/core';
import { LikeComponent } from './../like/like.component';

@Component({
  selector: 'tweet',
  template: `
  <div class="media">
  <div class="media-left">
    <a href="#">
      <img class="media-object" [src]="tweet.img" alt="...">
    </a>
  </div>
  <div class="media-body">
    <h4 class="media-heading">{{tweet.name}} <span>{{tweet.handle}}</span></h4>
    <div >{{tweet.content}}</div>
    <div>
    <like [likeCount]="tweet.likeCount"></like>
    </div>
  </div>
</div>
  `   
  ,
  declarations:[LikeComponent]
})
export class TweetComponent  { 
    @Input() tweet={
            name:"Mukul",
            handle:"@mukul",
            content:"some content",
            likeCount:20
        }

}

在这里我无法使用它,我试图将它注入到decleration中并且因为指令都不起作用,类型相关的错误显示在sublime中 Sublime error

希望在此获得一些澄清,谢谢

1 个答案:

答案 0 :(得分:2)

declarations:[LikeComponent]
@Component()中的

从2.0.0-RC.6开始消失