当我尝试通过输入将数组传递给组件时遇到问题。我在 home.ts 上 console.log 数组,我得到的是一个对象,然后,当我将它传递给组件时,在 component.ts 上我得到它是一个字符串。
我不知道我是否做得好
在组件 ts 我有这个:
@Component({
selector: 'micomponente',
templateUrl: 'micomponente.html'
})
export class MicomponenteComponent {
@Input() pra:any=[];
text: string;
constructor() {
console.log('Hello MicomponenteComponent Component');
this.text = '';
}
ngOnInit() {
console.log(typeof(this.pra)) //this is the red arrow on the picture
this.text = this.pra;
}
}
主页.html
<ion-content padding>
<micomponente pra="{{algo}}"></micomponente>
</ion-content>
家.ts
export class HomePage {
algo:any=[];
constructor(public navCtrl: NavController) {
this.algo.push(['1','fsa']);
this.algo.push(['2','fsd']);
console.log(this.algo)
}
}
这是控制台日志
要将数据传递给子组件,属性应该放在方括号 [] 中,属性应该放在引号中。在您的示例中,输入数据应该像这样传递:
<ion-content padding>
<micomponente [pra]="algo"></micomponente>
</ion-content>
有关如何将数据传入和传出子组件的信息,请参阅https://angular.io/guide/component-interaction。双花括号用于插值,将属性显示为 html 中的字符串。请在此处阅读有关插值的更多信息:https : //angular.io/guide/template-syntax#interpolation----。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句