3

I have a page that will be open by another application with parameterized URL.

Ex: sitename.com/language/myapplication?param1=xxx&param2=xxx

In my component .ts I have:

this.param1= this.route.snapshot.queryParamMap.get('param1');
this.param1= this.route.snapshot.queryParamMap.get('param2');

this.router.navigate(['/myapplication'],
  {
    queryParams: {
      param1: this.param1, param2: this.param1
    }
  });

I am using routing module as well:

const routes: Routes = [
  { path: '', redirectTo: '/myapplication', pathMatch: 'full' },
  { path: 'myapplication', component: myComponent },
  { path: '**', component: PageNotFoundComponent }
];

When I open the URL directly with the parameters it works fine.

Problem 1:

I have multi language on it, i18n, so when I change language via dropdown, the parameters disappear and it redirects to mysite.com/language/myapplication but I need something like sitename.com/fr/myapplication?param1=xxx&param2=xxx

Problem 2:

I want to force "page not found" in every scenario except when I have the URL with the parameters

Problem 3:

How can I transform those parameters from optional to required?

4
  • You can use AuthGaurd and the check there if Your URL has a parameter or not. ? Commented Jun 6, 2020 at 11:53
  • I will have an integration with Azure AD, maybe I can do it using it, I am not sure Commented Jun 6, 2020 at 11:56
  • 1
    I don't know about azure AD, but using AuthGuard you can check that. Commented Jun 6, 2020 at 11:57
  • Hi @FabioCardoso. Did you find a solution for this problem? I need the same than you and I don't know how to do it Commented Jan 28, 2021 at 16:51

2 Answers 2

3

Use route parameter instead of query string.

In the routing module:

const routes: Routes = [
  { path: '', redirectTo: '/myapplication', pathMatch: 'full' },
  { path: 'myapplication/:param1/:param2', component: myComponent },
  { path: '**', component: PageNotFoundComponent }
];

And in my-component.ts

this.param1= this.route.snapshot.paramMap.get('param1');
this.param2= this.route.snapshot.paramMap.get('param2');

If you need transform those parameters to optional add these lines to the routing module:

{ path: 'myapplication/:param1/:param2', component: myComponent },
{ path: 'myapplication/:param1', component: myComponent },
{ path: 'myapplication', component: myComponent },
Sign up to request clarification or add additional context in comments.

4 Comments

the url will be sitename.com/myapplication?param1=xxx&param2=xxx? because I tried this but I got the not found page
after debugging de snapshot object, queryParams have values but params is empty
The URL will be sitename.com/myapplication/xxx/xxx.
that is not the purpose. I need the name of the parameters. It is a requirement by the CI that will open the page.
1

Problem 1: It's not the job of angular router. You need to reload the browser with the right url for the language, do not use router for that, something like: location.assign(location.href.replace('/fr', '/en-US'));

Problem 2: A guard, but I prefer handling this directly in the component because you can use the same treatment for scenario without parameters, or parameter's value is not valid (e.g: user not found)

Problem 3: Same as problem 2. There is no such thing such as required parameter with angular router

8 Comments

I'm not using router for language... i18n works like that, I just have to register locale data in app.module add it to provider and it works like a charm. The problem is when I have parameters in the URL. If I don't have parameters I don't need to replace anything to get the language to work.
for problem 2, my routing module is working without guard if I don't use parameters... path: '**', component: PageNotFoundComponent does the job
You means you can change the language dynamically without reloading? You are not using AOT?
No, sorry, the page is reloading. For multi language I am using the basic implemention of i18n. angular.io/guide/i18n
How do you do the switching language with the dropdown? I don't see why you can't keep the parameters
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.