【问题标题】:How to Save user Activity in angular App? [closed]如何在 Angular App 中保存用户活动? [关闭]
【发布时间】:2021-07-11 18:55:57
【问题描述】:

我想获取并保存用户与 Angular 应用的交互, 以及应用程序与 API 的交互会将此日志添加到文件中。

【问题讨论】:

    标签: javascript html angularjs angular web


    【解决方案1】:

    如果我正确理解了您的问题,您可以在一处为用户页面导航和 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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多