【问题标题】:Exception self.context.XXX.subscribe is not a function异常 self.context.XXX.subscribe 不是函数
【发布时间】:2017-05-13 13:44:08
【问题描述】:

在我的 Angular 2 应用程序中,我需要将一个数组从一个组件传递到另一个组件。我有一个名为 SystemDynamicsComponent 的组件,它公开了一个数组 sdFactors

@Component({
   selector: "system-dynamics",
   templateUrl: "/app/component/main/template/system-dynamics.html"
})

export class SystemDynamicsComponent implements OnInit {
    private dialogDisplayed: boolean = false;
   @Output() sdFactors: any[] = [];

在名为InputModuleComponent的组件中我需要读取这个值如下:

模板:

<system-dynamics (sdFactors)="this.sdFactors"></system-dynamics>

组件:

export class InputModuleComponent implements OnInit {
...
sdFactors: any[] = [];

当启动输入模块组件时,我从 Angular 收到以下错误消息: TypeError: self.context.sdFactors.subscribe is not a function at Wrapper_SystemDynamicsComponent.subscribe (wrapper.ngfactory.js:38) at View_InputModuleComponent6.createInternal (component.ngfactory.js:3103) at View_InputModuleComponent6.AppView.create (core.umd.js:12262) at View_InputModuleComponent6.DebugAppView.create (core.umd.js:12666) at TemplateRef_.createEmbeddedView (core.umd.js:9320) at ViewContainerRef_.createEmbeddedView (core.umd.js:9552)

任何想法我错过了什么?

【问题讨论】:

    标签: angular angular2-components


    【解决方案1】:

    试试这个

    <system-dynamics (sdFactors)="sdFactors"></system-dynamics>
    

    而不是

    <system-dynamics (sdFactors)="this.sdFactors"></system-dynamics>
    

    您不需要在模板中添加this 进行绑定。同一类中的变量可以在模板中不使用this 的情况下访问。

    更新

    @output 用于事件绑定而不是属性绑定,您必须将@input 用于 passign 数组或一些变量值,所以像这样使用它

    <system-dynamics [sdFactors]="sdFactors"></system-dynamics>
    

    在你这样的组件中

    @Component({
       selector: "system-dynamics",
       templateUrl: "/app/component/main/template/system-dynamics.html"
    })
    
    export class SystemDynamicsComponent implements OnInit {
        private dialogDisplayed: boolean = false;
       @Input() sdFactors: any[] = [];
    

    【讨论】:

    • 我按照您的建议 (&lt;system-dynamics (sdFactors)="sdFactors"&gt;&lt;/system-dynamics&gt;) 更改了它,但问题仍然存在。
    • 请看我的更新
    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 2018-01-22
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多