ngx-cookieconsent icon indicating copy to clipboard operation
ngx-cookieconsent copied to clipboard

No Angular routing possible

Open Fluuub opened this issue 5 years ago • 7 comments

The link provided in NgcContentOptions does not allow Angular router to be used, sadly... As such, a local redirection reloads the complete Angular app which is not an expected behaviour. Would you think of adding such possibility to use routerLink? Thanks

Fluuub avatar Nov 13 '18 12:11 Fluuub

Hi @Fluuub ,

This is a valid request indeed. Have you try overriding the HTML element that defines that link? (via link sub-property of elements property inside NgcCookieConsentConfig object)?

const cookieConfig:NgcCookieConsentConfig = {
  cookie: {
    domain: 'localhost' // or 'your.domain.com' 
  },
  elements: {
    link: '<a aria-label="learn more about cookies" tabindex="0" class="cc-link" routerLink="/your-cookie-policy-route">{{link}}</a>'
  }
};

Try that and tell me if it works for you.

tinesoft avatar Nov 13 '18 13:11 tinesoft

Thanks tinesoft, but unfortunate... Just tried to change elements.messagelink w/ your suggestion but routerLink is just ignored...

Fluuub avatar Nov 13 '18 14:11 Fluuub

ok, i'll have a closer look tonight.

Stay tuned!

tinesoft avatar Nov 13 '18 15:11 tinesoft

Hey! I found this hack... This is quite ugly, but it works...

const cookieConfig:NgcCookieConsentConfig = {
  cookie: {domain: 'localhost'},
  elements: {messagelink: `
          <span id="cookieconsent:desc" class="cc-message">{{message}}&nbsp;
               <a id="cookieconsent:link" aria-label="learn more about cookies" tabindex="0" class="cc-link">{{link}}</a>
          </span>
     `}
};
this.ccService.init (cookieConfig);
document.getElementById('cookieconsent:link').addEventListener('click', (e) => this.router.navigate(['/fr/cookies']));

Fluuub avatar Nov 13 '18 16:11 Fluuub

Any other workaround apart from using JavaScript?

asad-c avatar Jun 14 '19 00:06 asad-c

Ok, I finally figured it out.

Angular 8.

My scenario: I developed an app with routing navigation, imported and configured CookieConsent to display banner and cookie page link in it. On localhost:4200 all is ok but when I uploaded app on webserver I redeived 404 page by attempting to show the page with the disclaimer (http://mysite/cookie).

So I resolved by using useHash: true in my @NgModule and href: "#/cookie" (with hash) in my href config and now all works fine (http://mysite/#/cookie).

// src/app/app-routing.module.ts
@NgModule({
  imports: [RouterModule.forRoot(routes, {useHash: true})],
...
})


// src/app/app.module.ts
const cookieConfig:NgcCookieConsentConfig = {
...
  content: {
    dismiss: 'OK',
    message: 'message text here',
    link: 'link text here',
    href: "#/cookie"
  }
};

hope it helps

ghost avatar Sep 21 '19 01:09 ghost

Any updates? Facing the same "problem" with my application right now. I think that would be a great enhancement

lovetodream avatar Jul 19 '20 14:07 lovetodream

I got one. First read more here: https://tinesoft.github.io/ngx-cookieconsent/doc/

and

Routes: { path: ":lang/cookies-policy", component: CookiesPolicyComponent, },

Module Declaration with hashes

@NgModule({ imports: [RouterModule.forRoot(routes, {useHash: true})], exports: [RouterModule] })

and App Module cookie config:

[...], elements:{ messagelink: ' <span id="cookieconsent:desc" class="cc-message">{{message}} <a aria-label="learn more about cookies" tabindex="0" class="cc-link" href="{{cookiePolicyHref}}" target="_self">{{cookiePolicyLink}}</a>, </span> ', }, content: { target: "_self", cookiePolicyLink: 'Cookie Policy', cookiePolicyHref: '#/${navigator.language}/cookies-policy', },

Remember, only target="_self" in this case. It will work🐙

Bosper avatar May 27 '23 00:05 Bosper