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组件 –