Angular2 - 类构造函数不能在没有'new'的情况下被调用

3

我正在尝试集成angular2-odata库。 我正在使用

    @Injectable()
    class MyODataConfig extends ODataConfiguration{
        baseUrl="http://localhost:54872/odata/";
    }

    bootstrap(app,[
        //some of my other providers etc.
        provide(ODataConfiguration, {useClass:MyODataConfig}),
        ODataServiceFactory,
    ]

问题在于当我尝试注入ODataServiceFactory时,我收到了以下错误:
异常:实例化ODataConfiguration时出错!(ClassService-> ODataServiceFactory-> ODataConfiguration)。
原始异常:TypeError:不能在没有“new”的情况下调用类构造函数ODataConfiguration
我已经搜索过,似乎在注入扩展类时存在问题,但我无法找到解决方法。任何帮助将不胜感激。

我认为你的类需要一个调用 super(); 的构造函数。 - Jack Guy
谢谢您的建议,但我尝试添加 constructor() { super(); } 后,异常仍然相同。 - Pavel Hornak
你能同时发布你的ODataServiceFactory实现吗? - Harry Ninh
您可以在 angular2-odata 库的 GitHub 上查看 ODataServiceFactory https://github.com/gallayl/angular2-odata/blob/master/odataservicefactory.ts。 - Pavel Hornak
1个回答

1

一个临时解决方法是在ODataConfiguration类上使用new,然后覆盖baseUrl

@Component({
    templateUrl: './categoryGridOData.component.html',
    selector: 'my-category-grid-odata',
    providers: [ { provide: ODataConfiguration, useFactory: () => {
        let odta = new ODataConfiguration();
        odta.baseUrl = 'http://services.odata.org/V4/Northwind/Northwind.svc';
        return odta; }
    }, ODataServiceFactory ],
    styleUrls: [ './carGrid.component.css']
})
export class CategoryGridODataComponent {

完整代码请参见:https://github.com/StefH/angular2-webpack-starter/blob/_stef_dev/src/app/car/categoryGridOData.component.ts#L18

更多细节请参见:https://github.com/gallayl/angular2-odata/issues/3


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接