1
我是新來的角2,目前用它進行試驗無法導入另一個組件,這是我進入角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
}
}
這裏的IAM不能夠使用它,我曾試圖decleration和兩個不工作的指令注入它,在崇高的顯示類型相關的錯誤
希望能在這裏得到一些澄清,感謝
更新您的文章有**的** LikeComponent代碼消失了。首先,你試圖達到什麼目標? – Aravind
肯定@Aravind它現在更新了,我試圖在tweet組件中導入likeComponent, LikeComponent最初是在應用程序模塊內部導入的,但是我想看看它是否可以在tweet組件內導入 –
刪除聲明:[LikeComponent] 。你需要在你的模塊中刪除thr組件 –