Angular 2 router alternate syntaxes for routerLink directive not working - angular2-routing

I'm trying to get the routerLink directive to work with matrix parameters. So far the only syntax variant that I've been able to get working is simply the path string.
<a routerLink="/state-scratchpad">Syntax 1 - works</a>
<a routerLink="['/state-scratchpad']">Syntax 2 - doesn't work</a>
<a routerLink="['/state-scratchpad', { tcomp: '1', tmake: '1-7' }]">Syntax 3 - doesn't work</a>
<a routerLink="/state-scratchpad;tcomp=1;tmake=8">Syntax 4 - doesn't work</a>
Navigating in the URL bar to /state-scratchpad;tcomp=1;tmake=8 also works.
Here are my routes:
const routes = [
{ path: '', pathMatch: 'full', redirectTo: '/welcome' },
{ path: 'welcome', component: WelcomeComponent },
{ path: 'state-scratchpad', component: StateScratchpadComponent, params: { tcomp: '', tmake: '' } }
];
I'm using Angular 2.0.0. According to the documentation on the angular.io site the various syntaxes for routerLink values should all work. But only the simplest syntax works. Here are the resulting error messages:
S1: Works
S2: Error: Cannot match any routes: '%5B'/state-scratchpad'%5D'
S3: Error: Cannot match any routes: '%5B'/state-scratchpad'%2C%20%7B%20tcomp%3A%20'1'%2C%20tmake%3A%20'1-7'%20%7D%5D'
S4: Error: Cannot match any routes: 'state-scratchpad%3Btcomp%3D1%3Btmake%3D8'
Any ideas?

First a router link looks like this
<a [routerLink]="['/state-scratchpad', tcomp, tmake]">
This will pass the base url and append the params.
Next your route should look like this
{ path: 'state-scratchpad/:tcomp/:tmake', component: StateScratchpadComponent}
Then when the component is loaded you access the params like this
constructor( private route: ActivatedRoute) {}
ngOnInit(){
this.route.snapshot.params["tcomp"];
this.route.snapshot.params["tmake"];
}

Related

`next-sitemap` duplicated language in alternate ref path (href)

..I have a Next.js application with multi-language support (English as the default language and German as the secondary one - English is on https://mywebsite.com and German on https://mywebsite.com/de).
I'm using next-sitemap to generate a sitemap for the page using alternate refs to link the English and German versions of the pages. The following is my next-sitemap config:
/** #type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: `https://mywebsite.com`,
generateRobotsTxt: true,
exclude: ['/app/*', '/social-redirect'],
robotsTxtOptions: {
policies: [
{
userAgent: '*',
[process.env.VERCEL_ENV !== 'preview' && process.env.VERCEL_ENV !== 'development'
? 'allow'
: 'disallow']: '/',
},
],
},
alternateRefs: [
{
href: 'https://mywebsite.com',
hreflang: 'en',
},
{
href: 'https://mywebsite.com/de',
hreflang: 'de',
},
],
};
In the generated sitemap the English entries of the sitemap look good. They have the correct alternate refs. But in the German entries of the sitemap, the alternate refs have the language in the path twice, so for example: https://mywebsite.com/de/de/blog. Is this an issue of next-sitemap or am I doing something wrong? I would be glad if someone could help me with that!
This is also happening to me. I think it is an issue of next-sitemap
In my case I have languages en and en-gb The urls generated are:
<xhtml:link rel="alternate" hreflang="en" href="http://localhost:3000/en/en-gb/blog/"/>
<xhtml:link rel="alternate" hreflang="en-gb" href="http://localhost:3000/en-gb/en-gb/blog/"/>
You can use transform prop of next-sitemap.config.js to configure a replacement
`transform: async (config, path) => {
return { ...config,
loc: path.replace('/en-gb/en-gb', '/en-gb')
}
}

Angular fails to fetch CSS file on navigation to nested route

When directly navigating to a child component in prod mode (ng serve --prod) it fails to load the CSS file, trying to fetch it from a nested path. For instance, navigating to "localhost:4200/doc/" the CSS Request URL is:
localhost:4200/doc/styles.6eab038f040a1c7c7ed6.css.
The error that is given:
Refused to apply style from 'http://localhost:4200/doc/styles.6eab038f040a1c7c7ed6.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
When accessing the root (localhost:4200) the CSS is loaded correctly. If you then navigate to the child component through the app itself, the problem does not occur.
It only occurs in prod mode, not on the development server. Changed nothing to the regular settings regarding styles.css, it is situated in the default folder.
"styles": [
"src/styles.css"
],
Anyone has any idea how this problem could be caused?
Edit
Found out that the problem lies in navigating to nested routes (helped by the reply of mikegross).
On a route without a nested parameter the problem does not occur, but on a route with a nested parameter (for instance 'doc/:id') it does.
Router module:
const routes: Routes = [
{ path: '', redirectTo: 'list', pathMatch: "full"},
{ path: 'list', component: ListComponent},
{ path: 'doc/:id', component: DocumentComponent},
{ path: '**', redirectTo: ''},
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Update (feb. 28)
Since it was still a small project I generated a new angular project, installed same dependencies and literally copy pasted the code.
Although I have no idea why, in the new project the problem does not occur. The only difference lies in a minor version bump (ng 9.0 to 9.1).
I know this is an older post, but I got around this by using query parameters instead of route parameters. example:
const routes: Routes = [
{ path: '', redirectTo: 'list', pathMatch: "full"},
{ path: 'list', component: ListComponent},
{ path: 'doc', component: DocumentComponent},
{ path: '**', redirectTo: ''},
];
(I removed the :id from "doc/:id")
Navigate to the route:
route this._router.navigate(['/doc'],{queryParams: {id: id}});
Then retrieve the query parameter:
this._activatedRoute.snapshot.queryParamMap.get('id');
Coming late to the party. I had the same error with React in 2022.
Initially, I declared the CSS file with a relative path, as follows:
<link rel="preload" href="./style/Avenir-Light.woff2" as="font" type="font/woff2" crossorigin>
After changing it to an absolute path, it worked
<link rel="preload" href="/style/Avenir-Light.woff2" as="font" type="font/woff2" crossorigin>
Hope it helps future readers.

Add a video background on a specific component/path

I am trying to set a full screen video background which I want to be only visible on the app "homepage" with no success yet. The first component rendered on the app is HomeComponent.
app-routing-module.ts (only path is shown)
const routes: Routes = [
{ path: '', redirectTo: '/accueil', pathMatch: 'full'},
{ path: 'accueil', component: HomeComponent },
{ path: 'ligne/:lineId', component: LineComponent },
{ path: 'ligne/:lineId/station/:stationName', component: StationComponent }
];
What I'd like to do is to have the video background only when I'm on /accueil but not on the other paths/components. Is there any way to do that ?
Thanks in advance.
You can add the router to your component that contains the div you want to set the background to the video
and then do something like this:
if(this.router.url===routeIWant){
this.custom-bg=true
}else{
this.custom-bg=false
}
And on the html the following div is the one you wanna set the bg
<div [ngClass]="{'customBgClass': custom-bg}">
And on the css
.customBgClass{
background: url(your-video-url)
}

Angular 2: How to use same child component with different data?

I have a header that has menu consist of categories which they are the same component but different query.
The first call from parent to child is executed perfectly but if i navigateto another category from inside this child component i get error!
I know this is something wrong with the link when i navigate inside this child which make [routerLink] updated instantly.
Also if i navigate from category child component to login component which is separated component will give me the same error!
Note: Same question has been asked in this link with no answer
Nacim Idjakirene Question
Router configuration
const APP_ROUTES: Routes = [
{path: 'landingpage', component: LandingpageComponent },
{path: '', component: IndexComponent, children: [
{path: '', component: HomeComponent},
{path: 'category/:cate', component: CategoryComponent },
{path: 'post/:id', component: PostComponent },
{path: 'profile/:id', component: ProfileComponent }
] },
{path: 'login', component: LogginComponent }
];
See the image
Problem Solved by me after a deep search. i saw the response of a person called
brandonroberts: your router link is invalid and you either need to set a terminal
route
and this solved my problem...
[routerLink]="['','category', cat.id]"

How to do multiple routing (router-outlet) in angular2 RC4?

I am finding problem while multiple routing, its loading in single router-oulet how to tell the component to load in specific named outlet? I found out that there is a property in RouterConfig from which i can use outlet to name the router-outlet but i don't know to link it with HTML.
https://angular.io/docs/ts/latest/api/router/index/RouterConfig-type-alias.html
Define a UI with two router outlets..
<div>
<router-outlet></router-outlet>
<router-outlet name="aux"></router-outlet>
</div>
Then have a router config that routes a component into that space.
[{
path: 'parent/:id',
children: [
{ path: 'a', component: MainChild },
{ path: 'b', component: AuxChild, outlet: 'aux' }
]
}]
NOTE: the parent route's id parameter will be provided to both children. Remove it if you aren't providing an id.

Resources