首页 文章

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

提问于
浏览
1

我是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 回答

  • 2
    declarations:[LikeComponent]
    

    @Component() 以来,自2.0.0-RC.6以来已经消失

相关问题