gpt4 book ai didi

debugging - 如何在 Angular 2 中以编程方式启用路由跟踪?

转载 作者:太空狗 更新时间:2023-10-29 17:01:26 25 4
gpt4 key购买 nike

我知道我可以在 Angular 2 路由器上启用跟踪:

export const routing: ModuleWithProviders = 
RouterModule.forRoot(routes, { enableTracing: true });

有没有办法以编程方式设置该值?

我有一个 config.json 文件,其中包含许多应用程序设置。我想包含一个 bool 属性,可用于控制是否启用跟踪。我宁愿客户没有能力修改包含我的路由的文件,但仍然有能力打开跟踪以帮助调试未被测试捕获的边缘情况。

我可以接受必须重新启动应用程序,但不能接受必须重建。

[Update] After looking at the router source, it doesn't look do-able without a pull request. Simple explanation of what the change would need to be:

  1. 在 router.ts 中,添加一个名为 tracing: boolean 的公共(public)属性
  2. 在 router_module.ts::setupRouter 中:

    • 改变

      if (opts.enableTracing) {

      router.tracing = opts.enableTracing</pre>

    • 更改可观察对象:

      router.events.subscribe(e => { ...

      router.events
      .filter(e => router.tracing)
      .subscribe(e => { ...</li>

3. 可能需要在跟踪属性上添加一些验证。

通过这些更改,可以导入 Router,然后设置 router.tracing 属性来打开和关闭它。

我不知道在没有订阅者的情况下发出所有事件与在过滤订阅的情况下发出所有事件之间的性能差异。

最佳答案

目前还没有明确的方法来以编程方式进行。我们的解决方法是只为 local development 启用它,以便我们可以获得所有详细信息/exception-stacktrace 等。类似于:

let routes = [{path: ..., component : ...}, ...];
RouterModule.forRoot(routes, {
enableTracing: /localhost/.test(document.location.host)
});

关于debugging - 如何在 Angular 2 中以编程方式启用路由跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com