在AngularJS打字稿拦截 [英] TypeScript interceptor in AngularJS

查看:113
本文介绍了在AngularJS打字稿拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题使用打字稿在AngularJS建立一个请求拦截

下面的代码片段工作,不工作的变体被注释掉。无论我在构造函数中的局部变量在要求未定义注入方法。

 模块服务
{
    出口类AuthInterceptor
    {
        公共静态工厂(TokenService:Services.ITokenService)
        {
            返回新AuthInterceptor(TokenService);
        }        构造函数(私人TokenService:Services.ITokenService)
        {
            this.request =(配置:ng.IRequestConfig)=>
            {
                config.headers = config.headers || {};
                如果(this.TokenService.IsAuthorised())
                    config.headers.Authorization ='承载'+ this.TokenService.Token;
                返回配置;
            };
        }        公众要求:(配置:ng.IRequestConfig)=> ng.IRequestConfig;/ *这是不工作        公共请求(配置)
        {
                    // this.TokenService是不确定的位置,以及$窗口或$ Q我试图注入
            config.headers = config.headers || {};
            如果(this.TokenService.Token!=)
                config.headers.Authorization ='承载'+ this.TokenService.Token;
            返回配置;
        }
* /    }
}angular.module(服务)
    的.config(($ httpProvider:ng.IHttpProvider)=>
    {
        $ httpProvider.interceptors.push(Services.AuthInterceptor.Factory);
    });


解决方案

这是错误的,因为这个。解决方法:

 公开请求=(配置)=>
    {
                // this.TokenService是不确定的位置,以及$窗口或$ Q我试图注入
        config.headers = config.headers || {};
        如果(this.TokenService.Token!=)
            config.headers.Authorization ='承载'+ this.TokenService.Token;
        返回配置;
    }

要了解你为什么需要这样的:<一href=\"https://www.youtube.com/watch?v=tvocUcbCupA&hd=1\">https://www.youtube.com/watch?v=tvocUcbCupA&hd=1

I'm having problems setting up a request interceptor in AngularJS using TypeScript

The following snippet works, not working variant is commented out. No matter what I inject in the constructor the local variables are undefined in the request method.

module Services
{
    export class AuthInterceptor
    {
        public static Factory(TokenService: Services.ITokenService)
        {
            return new AuthInterceptor(TokenService);
        }

        constructor(private TokenService: Services.ITokenService)
        {
            this.request = (config: ng.IRequestConfig) =>
            {
                config.headers = config.headers || {};
                if(this.TokenService.IsAuthorised())
                    config.headers.Authorization = 'Bearer ' + this.TokenService.Token;
                return config;
            };
        }

        public request: (config: ng.IRequestConfig)=>ng.IRequestConfig;

/* THIS IS NOT WORKING

        public request(config)
        {
                    // this.TokenService is undefined here as well as $window or $q which I tried to inject
            config.headers = config.headers || {};
            if(this.TokenService.Token != "")
                config.headers.Authorization = 'Bearer ' + this.TokenService.Token;
            return config;
        }
*/

    }
}

angular.module("Services")
    .config(($httpProvider: ng.IHttpProvider)=>
    {
        $httpProvider.interceptors.push(Services.AuthInterceptor.Factory);
    });

解决方案

It is because of the wrong this. Solution:

    public request = (config) =>
    {
                // this.TokenService is undefined here as well as $window or $q which I tried to inject
        config.headers = config.headers || {};
        if(this.TokenService.Token != "")
            config.headers.Authorization = 'Bearer ' + this.TokenService.Token;
        return config;
    }

To understand why you need this : https://www.youtube.com/watch?v=tvocUcbCupA&hd=1

这篇关于在AngularJS打字稿拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆