ngx-cookieconsent
ngx-cookieconsent copied to clipboard
No Angular routing possible
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
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.
Thanks tinesoft, but unfortunate... Just tried to change elements.messagelink w/ your suggestion but routerLink is just ignored...
ok, i'll have a closer look tonight.
Stay tuned!
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}}
<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']));
Any other workaround apart from using JavaScript?
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
Any updates? Facing the same "problem" with my application right now. I think that would be a great enhancement
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🐙