简体   繁体   English

Angular5-Toaster动态组件注入参数

[英]Angular5-Toaster Dynamic component inject parameters

Good afternoon, 下午好,

I am trying to create a dynamic template to display information from a dynamic component as listed in the NPM readme. 我正在尝试创建一个动态模板,以显示NPM自述文件中列出的动态组件中的信息。 https://www.npmjs.com/package/angular5-toaster https://www.npmjs.com/package/angular5-toaster

Example : 范例:

import {BodyOutputType} from 'angular5-toaster';

@Component({
  selector: 'dynamic-component',
  template: `<div>loaded via component</div>`
})
class DynamicComponent { }

var toast : Toast = {
    type: 'error',
    title: 'Title text',
    body: DynamicComponent,
    bodyOutputType: BodyOutputType.Component
};

this.toasterService.pop(toast);

My question is how would i pass parameters into the DynamicComponent. 我的问题是如何将参数传递给DynamicComponent。

Thanks 谢谢

I would suggest to share the data using Angular Service. 我建议使用Angular Service共享数据。

Step 1 : set the data want to pass in to the dynamic component before you load the Toast. 步骤1:在加载Toast之前,设置要传递到动态组件的数据。

    this.someService.data="some parameter"
    this.toasterService.pop(toast);

Step 2 : Once the component initialize you can read the value by injecting the service as a dependency on your dynamic component. 第2步:组件初始化后,您可以通过将服务作为对动态组件的依赖项进行注入来读取值。

ngOnInit(){
//read the data
console.log(this.someService.data)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM