S
S
Sashqa2019-12-24 15:17:40
Angular
Sashqa, 2019-12-24 15:17:40

Subscribe to the route?

Hello colleagues. I need your help. I've already broken my head.
I have a method for copying data from a form.
I am on the form No. 1, I press the button, the data is copied from the form and go to another route (form No. 2). It all works.
But if I need to copy the data and go to the same form, the route does not work.
In 1 case, I subscribe to params changes.
this.route.params
What can be done to somehow track if params has not changed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-12-24
@Sashqa

You either need to change the route, for example, by adding a hash or query param
. Or turn off component reuse, then the component will be re-initialized on each click. By placing this somewhere above, for example in app.component

constructor(
    private router: Router,
  ) {
    this.router.routeReuseStrategy.shouldReuseRoute = function () {
      return false;
    };
    this.router.events.subscribe((evt) => {
      if (evt instanceof NavigationEnd) {
        this.router.navigated = false;
      }
    });
  }

There is also an option to catch all clicks on routerLink and call navigate
upd.
in general, you should understand that you do not need to track the route, but perform your actions
1. when the component is initialized.
2. at the push of a button.
It is taken out to the method and called in the right place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question