在我的Interceptor
文件中,出现错误消息:
ERROR in ./src/app/auth/auth.interceptor.js 8:0
Module parse failed: Unexpected character '@' (8:0)
You may need an appropriate loader to handle this file type.
| import { UserService } from "./../shared/user.service";
|
> @Injectable()
| export class AuthInterceptor implements HttpInterceptor {
|
如何解决此问题?这是我的ts文件:
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { tap } from "rxjs/operatos";
import { Router } from "@angular/router";
import { UserService } from "./../shared/user.service";
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private userService:UserService, private router:Router){}
intercept(req:HttpRequest<any>, next:HttpHandler){
if(req.headers.get('noauth')){
return next.handle(req.clone());
}
else{
const clonedreq = req.clone({
headers:req.headers.set('Authorization', 'Bearer ' + this.userService.getToken())
})
return next.handle(clonedreq).pipe(tap(event => {}, err => {
if(err.error.auth === false ){
this.router.navigateByUrl('/login');
}
} ))
}
}
}
答案 0 :(得分:1)
我没有发现任何错误,此语法在Typescript中对我而言似乎有效。但是您似乎将其用作javascript
:将文件扩展名从js
更改为ts
,消息应该消失。