0

好的,所以我想要一个提供者,它可以让我关闭状态提供者的引用。所以,我有 JS 代码,我正在尝试将它迁移到 TS。这是我到目前为止所得到的,但由于某种原因,它一直抱怨没有这样的方法(属性'get'在类型'IReferenceProvider'上不存在。尝试将接口更改为'IReferenceProviderGetterInterface''get'方法似乎做得很好,但我失去了看起来不正确的“injectRef”方法,所以我相信我把接口放在了正确的位置)。任何人以前都遇到过这个问题,很高兴指出我做错了什么。尝试用谷歌搜索这个问题,但据我所知,一切似乎都还不错。无论如何,这里是片段:

export interface IReferenceProvider extends angular.IServiceProvider {
    injectRef(name: string, ref: angular.ui.IUrlRouterProvider | angular.ui.IStateProvider): void;
}

export interface IReferenceProviderGetterInterface {
    get(name: string): angular.ui.IUrlRouterProvider | angular.ui.IStateProvider;
}

class ReferenceProvider implements IReferenceProvider {
    private refs: { [key: string]: angular.ui.IUrlRouterProvider | angular.ui.IStateProvider } = {};

    $get(): IReferenceProviderGetterInterface {
        return {
            get: (name: string): angular.ui.IUrlRouterProvider | angular.ui.IStateProvider => {
                return this.refs[name];
            }
        };
    }

    injectRef(name: string, ref: angular.ui.IUrlRouterProvider | angular.ui.IStateProvider): void {
        this.refs[name] = ref;
    }
}

angular.module("app")
    .provider("refs", ReferenceProvider);
}

我使用它的方式是:

static $inject: Array<string> = ["refs"];

private $stateProvider: angular.ui.IStateProvider;

constructor(refs: IReferenceProvider) {
        this.$stateProvider = refs.get("$stateProvider");
...
}
4

1 回答 1

0

功能get上真的不存在IReferenceProvider,它只有功能$get

为了做你想做的事,你还需要将 get 函数放在 ReferenceProvider 本身上:

export interface IReferenceProvider extends angular.IServiceProvider, IReferenceProviderGetterInterface {
    injectRef(name: string, ref: angular.ui.IUrlRouterProvider | angular.ui.IStateProvider): void;
}

class ReferenceProvider implements IReferenceProvider {
    private refs: { [key: string]: angular.ui.IUrlRouterProvider | angular.ui.IStateProvider } = {};

    $get(): IReferenceProviderGetterInterface {
        return {
            get: (name: string): angular.ui.IUrlRouterProvider | angular.ui.IStateProvider => {
                return this.refs[name];
            }
        };
    }

    get: (name: string): angular.ui.IUrlRouterProvider | angular.ui.IStateProvider => {
        return this.refs[name];
    }

    injectRef(name: string, ref: angular.ui.IUrlRouterProvider | angular.ui.IStateProvider): void {
        this.refs[name] = ref;
    }
}

这将导致该功能在服务和提供者上可用。

于 2017-09-27T20:04:17.293 回答