Skip to content

Instantly share code, notes, and snippets.

View NetanelBasal's full-sized avatar
🎯
Focusing

Netanel Basal NetanelBasal

🎯
Focusing
View GitHub Profile
const routes = [
{
path: 'login',
component: LoginPageComponent,
canMatch: [authGuard({ isProtected: false })],
},
{
path: 'todos',
component: TodosPageComponent,
canMatch: [authGuard()],
import { inject } from '@angular/core';
import { CanMatchFn, Router } from '@angular/router';
import { AuthService } from './auth-service';
export function authGuard({
redirectTo,
isProtected = true,
}: {
redirectTo?: any[];
isProtected?: boolean;
const routes = [
{
path: 'somePath',
component: EditCmp,
canDeactivate: [(component: EditCmp) => !component.hasUnsavedChanges],
},
{ path: 'somePath', canActivate: [() => inject(MyDependency).canActivate()] },
];
import { repeat } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class MetricsService {
private http = inject(HttpClient);
getMetrics() {
return this.http.get('https://metrics')
.pipe(
repeat({ delay: 30_000 }),
import { repeat, filter, take, timer } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class FooService {
private http = inject(HttpClient);
doSometing(data) {
return this.http.post('https://foo', data)
.pipe(
// 👇👇👇
import { repeat, filter, take } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class FooService {
private http = inject(HttpClient);
doSometing(data) {
return this.http.post('https://foo', data)
.pipe(
repeat({ delay: 1000 }),
import { repeat } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class MetricsService {
private http = inject(HttpClient);
getMetrics() {
return this.http.get('https://metrics')
.pipe(
repeat({ delay: 30_000 }),
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(
withInterceptors([
(req, next) => {
console.log('Global interceptor');
return next(req);
},
])
),
import {
HTTP_INTERCEPTORS,
provideHttpClient,
withInterceptors,
withInterceptorsFromDi,
} from '@angular/common/http';
import { bootstrapApplication } from '@angular/platform-browser';
bootstrapApplication(AppComponent, {
providers: [
import {
provideHttpClient,
withInterceptors
} from '@angular/common/http';
import { bootstrapApplication } from '@angular/platform-browser';
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(
withInterceptors([