Angular2:从服务响应动态加载组件 [英] Angular2: Loading components dynamically from a service response

查看:78
本文介绍了Angular2:从服务响应动态加载组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这不是最好的解决方案,但我希望能够从JSON响应中动态加载组件,如下所示:

I'm aware this is not the best solution but I would like to be able to load components dynamically from a JSON response, something along these lines:

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1> {{component.title}} {{component.selector}}',
    providers: [AppService],
    directives: [ExampleComponent]
})
export class AppComponent implements OnInit {

    component:{};

    constructor(
        private _appService: AppService) {
    }

    ngOnInit() {
        this.component = this._appService.getComponent();
    }
}

app.service

@Injectable()
export class AppService {

    component = {
        title: 'Example component',
        selector: '<example></example>'
    }

    getComponent() {
        return this.component;
    }
}

example.component

@Component({
    selector: 'example',
    template: 'This a example component'
})
export class ExampleComponent  {
}

如果运行此示例,我的输出为<example></example>,但实际上未呈现该组件.我也尝试使用[innerHtml]="component.selector",但这也没有用.有人有想法或建议吗?

If I run this example, my output is <example></example> but it doesn't actually render the component. Also I've tried to use [innerHtml]="component.selector", but that also didn't work. Does anyone have an idea or suggestion?

推荐答案

更新

用于创建组件的代码有所更改. 一个有效的示例可以在 Angular 2 dynamic中找到用户点击所选组件的标签

The code to create components has changed a bit. A working example can be found in Angular 2 dynamic tabs with user-click chosen components

要动态插入组件,可以使用ViewContainerRef.createComponent()

To insert a component dynamically you can use ViewContainerRef.createComponent()

对于声明式方法,您可以使用诸如以下的帮助器组件

For a declarative approach you can use a helper component like

@Component({
  selector: 'dcl-wrapper',
  template: `<div #target></div>`
})
export class DclWrapper {
  @ViewChild('target', {read: ViewContainerRef}) target;
  @Input() type;
  cmpRef:ComponentRef;
  private isViewInitialized:boolean = false;

  constructor(private resolver: ComponentResolver) {}

  updateComponent() {
    if(!this.isViewInitialized) {
      return;
    }
    if(this.cmpRef) {
      this.cmpRef.destroy();
    }
   this.resolver.resolveComponent(this.type).then((factory:ComponentFactory<any>) => {
      this.cmpRef = this.target.createComponent(factory)
    });
  }

  ngOnChanges() {
    this.updateComponent();
  }

  ngAfterViewInit() {
    this.isViewInitialized = true;
    this.updateComponent();  
  }

  ngOnDestroy() {
    if(this.cmpRef) {
      this.cmpRef.destroy();
    }    
  }
}

另请参见 Angular 2动态标签用户单击选定的组件

在您的示例中,您可以像这样使用它

In you example you can use it like

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1> {{component.title}} <dcl-wrapper [type]="component.type"></dcl-wrapper>',
    providers: [AppService],
    directives: [ExampleComponent]
})
export class AppComponent implements OnInit {

    component:{};

    constructor(
        private _appService: AppService) {
    }

    ngOnInit() {
        this.component = this._appService.getComponent();
    }
}

import {ExampleComponent} from './example.component.ts';

@Injectable()
export class AppService {

    component = {
        title: 'Example component',
        type: ExampleComponent
    }

    getComponent() {
        return this.component;
    }
}

这篇关于Angular2:从服务响应动态加载组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆