首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义的未分配给ComponentFactory<any> 'DynamicComponent‘

未定义的未分配给ComponentFactory<any> 'DynamicComponent‘
EN

Stack Overflow用户
提问于 2017-08-21 16:01:24
回答 1查看 247关注 0票数 1

在寻找将MVC视图呈现到角2的过程中,最终得到了这个指令

代码语言:javascript
复制
export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any>> {
  const cmpClass = class DynamicComponent {};
  const decoratedCmp = Component(metadata)(cmpClass);

  @NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp] })
  class DynamicHtmlModule { }

  return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule)
    .then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => {
      return moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp);
    });
}

所有的工作都很好,我的View呈现如我所愿,但几秒钟后,这个错误会弹出。

我的组件代码:

代码语言:javascript
复制
 import {
    Component,
    Directive,
    NgModule,
    Input,
    ViewContainerRef,
    Compiler,
    ComponentFactory,
    ModuleWithComponentFactories,
    ComponentRef,
    ReflectiveInjector, OnInit, OnDestroy, Type
} from '@angular/core';

import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { Http } from "@angular/http";
import 'rxjs/add/operator/map';

export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any>> {   
    const cmpClass = class DynamicComponent { };
    const decoratedCmp = Component(metadata)(cmpClass);

    @NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp] })
    class DynamicHtmlModule { }

    return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule)
        .then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => {
            return moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp);
        });
}

@Directive({ selector: 'mvc-partial' })
export class RenderingViewDynamic implements OnInit, OnDestroy {
    html: string = '<p></p>';
    @Input() url: string;
    cmpRef: ComponentRef<any>;

    constructor(private vcRef: ViewContainerRef, private compiler: Compiler, private http: Http) { }

    ngOnInit() {
        this.http.get(this.url)
            .map(res => res.text())
            .subscribe(
            (html) => {
                this.html = html;
                if (!html) return;

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

                const compMetadata = new Component({
                    selector: 'dynamic-html',
                    template: this.html,
                });

                createComponentFactory(this.compiler, compMetadata)
                    .then(factory => {
                        const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
                        this.cmpRef = this.vcRef.createComponent(factory, 0, injector, []);
                    });
            },
            err => console.log(err),
            () => console.log('MvcPartial complete')
            );

    }

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

我怎么能克服这一切?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-21 18:46:57

我认为问题在于您定义了一个返回Promise<ComponentFactory<any>>的方法,但是在方法中使用了find函数,它可以返回undefined

find()方法返回数组中满足所提供的测试函数的第一个元素的值。否则,将返回未定义的

这就是TS抱怨的原因。只需修改方法调用:

代码语言:javascript
复制
createComponentFactory(...): `Promise<ComponentFactory<any>> | Promise<undefined>` { ... }

这里也是一样的事情:

代码语言:javascript
复制
this.cmpRef = this.vcRef.createComponent(factory, 0, injector, []);
                    });

createComponent只能使用ComponentFactory<C>,但是通过您的实现,可以传递undefined。可能添加一个工厂是否存在的检查,或者如果您确信它永远不会是未定义的,请使用非零断言操作符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45801433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档