【发布时间】:2019-08-23 03:39:02
【问题描述】:
我正在使用示例ng6-file-man-test 为我的应用程序创建文件上传功能,该示例使用ng6-file-man 模块来启用各种文件/文件夹操作功能。根据我的应用程序要求,我还使用拦截器来请求向它们添加令牌。拦截器适用于大多数请求,但不适用于上传和下载请求。
jwt.interceptor.ts
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse }
from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const duplicate = request.clone({ params: request.params.set('token', 'jwt-token') });
return next.handle(duplicate);
}
}
我没有对示例中给出的 AppComponent 进行任何更改。
以下是添加了拦截器的app.module.ts。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';
import { FileManagerModule } from 'ng6-file-man';
import { JwtInterceptor } from '../_helpers/jwt.interceptor';
@NgModule({
declarations: [
AppComponent
],
imports: [
HttpClientModule,
BrowserModule,
FileManagerModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
文件管理器组件如下
export class FileManagerComponent implements OnInit, AfterContentChecked {
tree: TreeModel;
appLanguage = 'it';
constructor() {
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
const treeConfig: ConfigInterface = {
baseURL: 'https://backend.com/',
api: {
listFile: 'api/file/list',
uploadFile: 'api/file/upload?token=' + currentUser.access_token,
downloadFile: 'api/file/download',
deleteFile: 'api/file/remove',
createFolder: 'api/file/directory',
renameFile: 'api/file/rename',
searchFiles: 'api/file/search'
},
options: {
allowFolderDownload: false,
showFilesInsideTree: true
}
};
this.tree = new TreeModel(treeConfig);
}
itemClicked(event: any) {
console.log(event);
}
ngAfterContentChecked() {
console.log('after');
}
ngOnInit() {
console.log('before');
let container = document.querySelector('.file-manager-left');
}
}
我想知道是否有可能的方法可以拦截模块向后端发出的upload 和download 请求。
【问题讨论】:
标签: angular angular-http-interceptors