【发布时间】:2021-07-11 18:55:57
【问题描述】:
我想获取并保存用户与 Angular 应用的交互, 以及应用程序与 API 的交互会将此日志添加到文件中。
【问题讨论】:
标签: javascript html angularjs angular web
我想获取并保存用户与 Angular 应用的交互, 以及应用程序与 API 的交互会将此日志添加到文件中。
【问题讨论】:
标签: javascript html angularjs angular web
如果我正确理解了您的问题,您可以在一处为用户页面导航和 API 调用编写通用逻辑。
对于页面导航,订阅路由器事件。例如。
constructor(router:Router) {
router.events.subscribe(event => {
if(event instanceof NavigationStart) {
}
if(event instanceof NavigationEnd) {
// log record to file here --> 'user navigated to XYZ page.'
}
// NavigationCancel
// NavigationError
// RoutesRecognized
}
});
对于API调用,可以实现HTTP拦截器。
@NgModule({
...
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptorService,
multi: true
}]
})
export class AppModule { }
@Injectable()
export class MyInterceptor implements HttpInterceptor {
intercept( req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// log record to file --> 'calling api {url}'
return next.handle(req);
}
}
【讨论】: