Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created January 22, 2025 10:57
Show Gist options
  • Save Armenvardanyan95/731d8677e58cc81977e927b3fb4ef3fd to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/731d8677e58cc81977e927b3fb4ef3fd to your computer and use it in GitHub Desktop.
// instead of this
@Component({
template: `
@switch (country.value()) {
@case ('UK') {
<app-uk-payroll/>
}
@case ('USA') {
<app-usa-payroll/>
}
@case ('Canada') {
<app-canada-payroll/>
}
}
`,
})
export class ShellComponent {
readonly #userService = inject(UserService);
country = rxResource({
loader: () => this.#userService.getCountryOfResidence(),
});
}
// do this
export function matchCountryPayroll(country: string): CanMatchFn {
return () => {
const userService = inject(UserService);
return userService.getCountryOfResidence().pipe(
map((userCountry) => userCountry === country)
);
};
}
export const routes: Routes = [
{
path: 'payroll',
canMatch: [matchCountryPayroll('UK')],
component: UKPayrollComponent
},
{
path: 'payroll',
canMatch: [matchCountryPayroll('USA')],
component: USAPayrollComponent
},
{
path: 'payroll',
canMatch: [matchCountryPayroll('Canada')],
component: CanadaPayrollComponent
},
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment