0

如果它与某人的问题重复,我很抱歉。但是我找不到我的问题的解决方案。

Angular 7. 我需要通过 将我的变量输入@Input到 ng-container 中ngComponentOutlet正如文件所说,有一个选项可以通过Injector. 如果它只是 a stringor也没关系number。但是我有一点复杂的事情:如果我想在注入类时有更复杂的逻辑怎么办:例如绑定一个Observable<SomeModel>我想从ngrx商店获取的?或者订阅它?

在这里,我遇到了以下问题:Cannot read property 'select' of undefined

@Injectable()
export class Greeter {
    suffix$;
    constructor(private store: Store<fromStore.State>) {
        this.suffix$ = this.store.select(fromStore.getSuffix);
    }
}

@Component({
    selector: 'complete-component',
    template: `Complete: {{ greeter.suffix$ | async }}`
})
export class CompleteComponent {
    constructor(public greeter: Greeter) {
        this.greeter.suffix$.subscribe(data => console.log('data', data));
        // not working
    }
}

@Component({
    selector: 'ng-component-outlet-complete-example',
    template: `
        <ng-container *ngComponentOutlet="CompleteComponent; 
                                        injector: myInjector;"
    })
export class NgTemplateOutletCompleteExample {
    CompleteComponent = CompleteComponent;
    myInjector: Injector;

    constructor(injector: Injector) {
        this.myInjector =
            Injector.create({providers: [{provide: Greeter, deps: []}], parent: injector});
    }
}

我究竟做错了什么?

4

0 回答 0