Andrew See
Andrew See

Reputation: 1122

Is it possible to rewrite URL using <app-route>?

Consider I want to rewrite a URL from /register to /tenant/register, where:-

Is it possible to rewrite URL using <app-route>? So that it can sort of have a virtual path and redirect to app-route accordingly with certain rules.

Upvotes: 3

Views: 346

Answers (2)

Andrew See
Andrew See

Reputation: 1122

Taking PSK as an example, we need to add a path-changed listener in <app-location> as follow:-

<app-location id="location"
    route="{{route}}"
    url-space-regex="^[[rootPath]]"
    on-path-changed="rewritePath">
</app-location>

Then define the rules for the rewritePath() as follow:-

/**
 * Rewrite path before passing to <app-route>
 */
rewritePath() {
  let location = this.$.location;

  let path = location.path;
  if (path == '/register') {
    location.path = '/tenant/register';
  }
}

Upvotes: 1

Cappittall
Cappittall

Reputation: 3441

Regarding the head of your question, add :

<app-location route="{{newRoute}}"></app-location>

top of your custom element and define a new route dynamically at your function as:

this.set('newRoute.path', "/tenant/register");// That you really want to go.

this.set method will direct your new target something like you have pressed <a href='/tenant/register'>Tenant/Register </a>

Upvotes: 0

Related Questions