简体   繁体   English

如何摆脱TypeScript中的$ injector类型错误?

[英]How to get rid of $injector type errors in TypeScript?

I have the following in my AngularJS Code: 我的AngularJS代码中有以下内容:

this.$injector.get('$state').current.name !== 'login'

However, it throws me an error of: 但是,它给我一个错误:

error TS2339: Property 'current' does not exist on type '{}'. 错误TS2339:类型“{}”上不存在属性“当前”。

Do you have any idea how to fix this issue? 你知道如何解决这个问题吗?


UPDATE UPDATE

I've tried this: 我试过这个:

export namespace NewInjector extends angular.auto.IInjectorService {
    current: any;
};

without any success. 没有任何成功。

The value returned by get() method of the $injector is any.. which does not have current property... so we just have to assert the compiler, that we know what will be returned (instead of any ) $injectorget()方法返回的值是任何..它没有current属性...所以我们只需断言编译器,我们知道将返回什么(而不是any

var state = this.$injector.get('$state') as ng.ui.IStateService;
var isNotLogin = state.current.name !== "login";
...

Another notation 另一种表示法

var state = this.$injector.get<ng.ui.IStateService>('$state');

And a quick overview of the IInjectorService 并快速概述了IInjectorService

interface IInjectorService {
    ...
    get<T>(name: string, caller?: string): T;
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM