【问题标题】:Angular 2: uiRouter get current state params in root componentAngular 2:uiRouter 在根组件中获取当前状态参数
【发布时间】:2017-05-25 22:59:46
【问题描述】:

如何在根组件中获取参数? (app.component.ts)

我有这样的 app.component.ts(我正在使用 Angular/Cli):

...

import {Transition} from "@uirouter/angular";
...

export class AppComponent {
  id: any;

  constructor(private trans: Transition) {
    this.id = trans.params().someId;
  }
}

但我明白了:

ERROR Error: No provider for Transition!

但是,如果我在任何内部组件(具有路由)中使用相同的逻辑 - 一切都很好。我做错了什么?

还有!

我正在使用ngx-restangular。我在app.module.ts:

// Function for settting the default restangular configuration
export function RestangularConfigFactory (RestangularProvider, authService) {
  RestangularProvider.setBaseUrl('http://api.test.com/v1');

  // This function must return observable
  var refreshAccesstoken = function () {
    // Here you can make action before repeated request
    return authService.functionForTokenUpdate();
  };

  RestangularProvider.addErrorInterceptor((response, subject, responseHandler) => {
    if (response.status === 403) {

      /*Here somehow I need to get route params too, is it possible, and how?*/

      refreshAccesstoken()
      .switchMap(refreshAccesstokenResponse => {
        //If you want to change request or make with it some actions and give the request to the repeatRequest func.
        //Or you can live it empty and request will be the same.

        // update Authorization header
        response.request.headers.set('Authorization', 'Bearer ' + refreshAccesstokenResponse)

        return response.repeatRequest(response.request);
      })
      .subscribe(
        res => responseHandler(res),
        err => subject.error(err)
      );

      return false; // error handled
    }
    return true; // error not handled
  });
}

// AppModule is the main entry point into Angular2 bootstraping process
@NgModule({
  bootstrap: [ AppComponent ],
  imports: [ 
    // Importing RestangularModule and making default configs for restanglar
    RestangularModule.forRoot([authService], RestangularConfigFactory),
  ],
})

我如何才能获得路由参数?

【问题讨论】:

  • 请不要在不合适的地方使用抽象。如果Slib 是某个特定的库(perfectScrollbar),请明确说明,因此答案可能适用于这个特定的库。整个问题中没有任何内容对 JS 库是通用的。 typeScript 中 ... 的语法是什么 - 它与 JavaScript 中的相同。 TS 是 JS 的超集。目前还不清楚问题是什么。
  • @estus 你错了,很多库都使用相同的结构
  • 什么构造?
  • @estus someElement = new SomePlugin()
  • 是的,是构造函数调用。在 JS 和 TS 中都是一样的。但它与所有那些“更新方法”的东西有什么关系呢?所有$('#container').perfectScrollbar('update')SLib('update') 都是完全不同的东西,它们高度特定于特定的库。抱歉,不清楚你在问什么。这个问题在你的脑海中可能是有意义的,但对于回答者来说却不是,这使你成为唯一能够理解和回答它的人。

标签: javascript angular typescript angular-ui-router restangular


【解决方案1】:

选项 1:路由到根组件

如果你目前在 index.html 中有:

<root-component></root-component> <!-- has a ui-view inside it -->

bootstrap: RootComponent

您可以切换到引导 UIView:

<ui-view></ui-view>

bootstrap: UIView

然后路由到你的根组件

const rootState = { component: RootComponent }

那么你的根组件将能够注入初始的Transition

查看示例应用以获取示例:

https://github.com/ui-router/sample-app-angular/blob/e8f8b6aabd6a51bf283103312930ebeff52fe4c3/src/app/app.module.ts#L37

https://github.com/ui-router/sample-app-angular/blob/e8f8b6aabd6a51bf283103312930ebeff52fe4c3/src/app/app.states.ts#L7-L18

选项2:使用transition start observable

class RootComponent {
  constructor(router: UIRouter) {
    this.subscription = router.globals.start$.subscribe((trans: Transition) => console.log(trans)))
  }
  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

选项 3:使用过渡挂钩

如果您尝试对初始转换执行一些操作,请在转换挂钩中执行此操作

function configFn(router: UIRouter) {
  router.transitionService.onStart({}, function(trans: Transition) {
    if (trans.$id === 0) {
      return somepromise; // Allows async, etc
    }
  }
}

【讨论】:

猜你喜欢
  • 2019-07-04
  • 2021-09-01
  • 2018-07-23
  • 2016-12-02
  • 2014-01-06
  • 1970-01-01
  • 2017-07-21
  • 2019-11-02
相关资源
最近更新 更多