我试图创建一个HttpInterceptor来为每个发生的http添加一些授权标头.我需要从名为AuthService的服务中获取标头.以下是代码:
拦截器:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { AuthService } from './auth.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) { }
}
Run Code Online (Sandbox Code Playgroud)
AuthService:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class AuthService {
constructor(private http: HttpClient) { }
}
Run Code Online (Sandbox Code Playgroud)
的AppModule:
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
}, AuthService]
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误:提供程序解析错误:无法实例化循环依赖项!InjectionToken_HTTP_INTERCEPTORS("[ERROR - >]"):在./AppModule@-1:-1中的NgModule AppModule中 …
我正在尝试扩展express.Request上可用的express的Request对象,以便它在会话中包含一些其他数据。我尝试创建自己的类型文件(d.ts)并使用以下代码:
import * as express from 'express';
declare module 'express' {
export interface Request {
session: express.Request['session'] & {
myOwnData: string;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
'session' is referenced directly or indirectly in its own type annotation.
Run Code Online (Sandbox Code Playgroud)
实现这一点的正确方法是什么?
我刚刚完成了我正在使用 angular 4 创建的应用程序的前端部分。我现在正在创建一个管理面板,该面板将有一个单独的登录页面和两个或三个组件。起初我创建了一个新模块并使用路由器创建了一些“/admin”路由。问题是应用程序组件具有我不需要的标头。使用 *ngIf 来检查当前 URL 似乎太过分了。为管理页面简单地创建第二个角度应用程序更好吗?最好的方法是什么?
我在打字文件中有以下内容:
interface A {
anObject: {
data: string;
};
someOtherData: boolean;
}
Run Code Online (Sandbox Code Playgroud)
现在我想更改接口,以便anObject也包含data2。我希望最终的形式是
interface A {
anObject: {
data: string;
data2: string;
};
someOtherData: boolean;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这样做,但失败了:
interface A {
anObject: {
data2: string;
}
}
Run Code Online (Sandbox Code Playgroud)
取而代之的是anObject兼具数据和数据2,它只有数据2。无论如何要保留原始密钥吗?