以下是具有@Input()
属性的组件的代码。
import { Component } from '@angular/core';
@Component({
selector: 'cd',
template: `
<h2>{{input1}}</h2>
`
})
export class cdComponent{
@Input() input1: string;
}
我想在其他组件中使用此组件,下面将介绍父组件的代码。
import { Component } from '@angular/core';
@Component({
selector:'main',
template: `
<cd [input1]="test"></cd>
<h2> Main Route </h2>
`
})
export class MainComponent{
}
当MainComponent
显示在浏览器中时,我期望子组件中的“测试”文本也能显示。
但是没有显示“测试”文本。任何帮助,将不胜感激。
它不显示,因为你逝去的命名变量test
来input1
的,但它不存在。当您使用[]
将值传递给输入时,它会查找名为的变量test
,我的猜测是您只想传递string test
。有两种方法可以做到这一点:
<cd [input1]="'test'"></cd>
或者:
<cd input1="test"></cd>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句