Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 将类型传递给组件并将该类型转发给工厂_Angular_Typescript - Fatal编程技术网

Angular 将类型传递给组件并将该类型转发给工厂

Angular 将类型传递给组件并将该类型转发给工厂,angular,typescript,Angular,Typescript,我正在尝试使用Angular 2和TypeScript创建工厂方法,但我显然没有正确的方法,因为我的TSC编译器抛出了一个错误: error TS1005: ',' expected. 我正在尝试将类型作为@Input()传递给如下组件: <custom-user [componentType]="CreateMeComponent"></custom-user> // component.directive.ts @Directive({ selector

我正在尝试使用Angular 2和TypeScript创建工厂方法,但我显然没有正确的方法,因为我的TSC编译器抛出了一个错误:

error TS1005: ',' expected.
我正在尝试将类型作为
@Input()
传递给如下组件:

<custom-user [componentType]="CreateMeComponent"></custom-user>
// component.directive.ts 
@Directive({
    selector: '[componentAnchor]'
})
export class ComponentDirective {
    constructor(
        private viewContainer: ViewContainerRef,
        private componentFactoryResolver: ComponentFactoryResolver
    ) {}

    public createComponent<MyComponent>() {  
        this.viewContainer.clear();

        let componentFactory =
            this.componentFactoryResolver.resolveComponentFactory(MyComponent);
        let componentRef = this.viewContainer.createComponent(componentFactory);

        return componentRef;
    }
}

我有以下代码:

// custom-user.component.ts
export class GalleriaCustomUserComponent implements OnChanges {
    @Input() componentType: any; // is there a type for types, other than any?
    @ViewChild(ComponentDirective) componentAnchor: ComponentDirective;

    ngAfterViewInit() {
        this.componentAnchor
            .createComponent<this.componentType>(); // this line throws the ',' error
    }
}
//custom-user.component.ts
导出类GalleriaCustomUserComponent实现OnChanges{
@Input()componentType:any;//除了any之外,还有其他类型吗?
@ViewChild(ComponentDirective)componentAnchor:ComponentDirective;
ngAfterViewInit(){
这是一个组件锚
.createComponent();//此行抛出“,”错误
}
}
指令如下所示:

<custom-user [componentType]="CreateMeComponent"></custom-user>
// component.directive.ts 
@Directive({
    selector: '[componentAnchor]'
})
export class ComponentDirective {
    constructor(
        private viewContainer: ViewContainerRef,
        private componentFactoryResolver: ComponentFactoryResolver
    ) {}

    public createComponent<MyComponent>() {  
        this.viewContainer.clear();

        let componentFactory =
            this.componentFactoryResolver.resolveComponentFactory(MyComponent);
        let componentRef = this.viewContainer.createComponent(componentFactory);

        return componentRef;
    }
}
//component.directive.ts
@指示({
选择器:“[componentAnchor]”
})
导出类组件指令{
建造师(
私有viewContainer:ViewContainerRef,
专用componentFactoryResolver:componentFactoryResolver
) {}
公共createComponent(){
this.viewContainer.clear();
让零部件工厂=
此.componentFactoryResolver.resolveComponentFactory(MyComponent);
让componentRef=this.viewContainer.createComponent(componentFactory);
返回组件REF;
}
}

这可能吗?或者这是一个糟糕的设计?

您不能在视图绑定中使用类型名称。仅支持对组件实例或模板变量的引用。将类型指定给组件的属性,然后可以使用它:

<custom-user [componentType]="createMeComponent"></custom-user>

谢谢你的回答。在这种情况下,是否可以根据类型的名称获取可以传递给泛型方法的类型?对不起,我对TS泛型了解不多。我并没有用TS和Angular2.Ok。或者,是否可以使用类型名称而不是类型本身的字符串调用
ComponentFactoryResolver.resolveComponentFactory()
?否,不支持。您可以维护从字符串到类型的映射(例如使用共享服务),以便能够自己从字符串映射到
类型。