-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(angular-query): add routing example (#6873)
- Loading branch information
Showing
43 changed files
with
914 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = {} | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "Node.js", | ||
"image": "mcr.microsoft.com/devcontainers/javascript-node:18" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = {} | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Angular Query router example | ||
|
||
To run this example: | ||
|
||
- `npm install` or `yarn` or `pnpm i` | ||
- `npm run start` or `yarn start` or `pnpm start` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{ | ||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", | ||
"version": 1, | ||
"cli": { | ||
"packageManager": "pnpm" | ||
}, | ||
"newProjectRoot": "projects", | ||
"projects": { | ||
"router": { | ||
"projectType": "application", | ||
"schematics": { | ||
"@schematics/angular:component": { | ||
"inlineTemplate": true, | ||
"inlineStyle": true, | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:class": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:directive": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:guard": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:interceptor": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:pipe": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:resolver": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:service": { | ||
"skipTests": true | ||
} | ||
}, | ||
"root": "", | ||
"sourceRoot": "src", | ||
"prefix": "app", | ||
"architect": { | ||
"build": { | ||
"builder": "@angular-devkit/build-angular:application", | ||
"options": { | ||
"outputPath": "dist/router", | ||
"index": "src/index.html", | ||
"browser": "src/main.ts", | ||
"polyfills": ["zone.js"], | ||
"tsConfig": "tsconfig.app.json", | ||
"assets": ["src/favicon.ico", "src/assets"], | ||
"styles": ["src/styles.css"], | ||
"scripts": [] | ||
}, | ||
"configurations": { | ||
"production": { | ||
"budgets": [ | ||
{ | ||
"type": "initial", | ||
"maximumWarning": "500kb", | ||
"maximumError": "1mb" | ||
}, | ||
{ | ||
"type": "anyComponentStyle", | ||
"maximumWarning": "2kb", | ||
"maximumError": "4kb" | ||
} | ||
], | ||
"outputHashing": "all" | ||
}, | ||
"development": { | ||
"optimization": false, | ||
"extractLicenses": false, | ||
"sourceMap": true | ||
} | ||
}, | ||
"defaultConfiguration": "production" | ||
}, | ||
"serve": { | ||
"builder": "@angular-devkit/build-angular:dev-server", | ||
"configurations": { | ||
"production": { | ||
"buildTarget": "router:build:production" | ||
}, | ||
"development": { | ||
"buildTarget": "router:build:development" | ||
} | ||
}, | ||
"defaultConfiguration": "development" | ||
}, | ||
"extract-i18n": { | ||
"builder": "@angular-devkit/build-angular:extract-i18n", | ||
"options": { | ||
"buildTarget": "router:build" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@tanstack/query-example-angular-router", | ||
"type": "module", | ||
"scripts": { | ||
"ng": "ng", | ||
"start": "ng serve", | ||
"build": "ng build", | ||
"watch": "ng build --watch --configuration development" | ||
}, | ||
"private": true, | ||
"dependencies": { | ||
"@angular/animations": "^17.1.3", | ||
"@angular/common": "^17.1.3", | ||
"@angular/compiler": "^17.1.3", | ||
"@angular/core": "^17.1.3", | ||
"@angular/forms": "^17.1.3", | ||
"@angular/platform-browser": "^17.1.3", | ||
"@angular/platform-browser-dynamic": "^17.1.3", | ||
"@angular/router": "^17.1.3", | ||
"@tanstack/angular-query-experimental": "^5.20.1", | ||
"rxjs": "^7.8.1", | ||
"tslib": "^2.6.2", | ||
"zone.js": "^0.14.3" | ||
}, | ||
"devDependencies": { | ||
"@angular-devkit/build-angular": "^17.1.3", | ||
"@angular/cli": "^17.1.3", | ||
"@angular/compiler-cli": "^17.1.3", | ||
"@tanstack/angular-query-devtools-experimental": "^5.20.1", | ||
"typescript": "5.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<p> | ||
As you visit the posts below, you will notice them in a loading state the | ||
first time you load them. However, after you return to this list and click on | ||
any posts you have already visited again, you will see them load instantly and | ||
background refresh right before your eyes! | ||
<strong> | ||
(You may need to throttle your network speed to simulate longer loading | ||
sequences) | ||
</strong> | ||
</p> | ||
<angular-query-devtools initialIsOpen /> | ||
<router-outlet /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Component } from '@angular/core' | ||
import { RouterOutlet } from '@angular/router' | ||
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental' | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
standalone: true, | ||
imports: [AngularQueryDevtools, RouterOutlet], | ||
templateUrl: './app.component.html', | ||
styles: [], | ||
}) | ||
export class AppComponent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { provideHttpClient, withFetch } from '@angular/common/http' | ||
import { provideRouter, withComponentInputBinding } from '@angular/router' | ||
import { | ||
QueryClient, | ||
provideAngularQuery, | ||
} from '@tanstack/angular-query-experimental' | ||
|
||
import { routes } from './app.routes' | ||
import type { ApplicationConfig } from '@angular/core' | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideAngularQuery(new QueryClient()), | ||
provideHttpClient(withFetch()), | ||
provideRouter(routes, withComponentInputBinding()), | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { Route } from '@angular/router' | ||
|
||
// loadComponent lazily loads the component | ||
// when the component is the default export, there is no need to handle the promise | ||
|
||
export const routes: Array<Route> = [ | ||
{ | ||
path: '', | ||
loadComponent: () => import('./components/posts.component'), | ||
}, | ||
{ | ||
path: 'post/:postId', | ||
loadComponent: () => import('./components/post.component'), | ||
}, | ||
] |
19 changes: 19 additions & 0 deletions
19
examples/angular/router/src/app/components/post.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<div> | ||
<div> | ||
<a routerLink="/" href="#">Back</a> | ||
</div> | ||
@if (postQuery.status() === 'pending') { | ||
Loading... | ||
} @else if (postQuery.status() === 'error') { | ||
Error: {{ postQuery.error()?.message }} | ||
} | ||
@if (postQuery.data(); as post) { | ||
<h1>{{ post.title }}</h1> | ||
<div> | ||
<p>{{ post.body }}</p> | ||
</div> | ||
@if (postQuery.isFetching()) { | ||
Background Updating... | ||
} | ||
} | ||
</div> |
38 changes: 38 additions & 0 deletions
38
examples/angular/router/src/app/components/post.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
EventEmitter, | ||
Output, | ||
inject, | ||
input, | ||
} from '@angular/core' | ||
import { RouterLink } from '@angular/router' | ||
import { | ||
injectQuery, | ||
injectQueryClient, | ||
} from '@tanstack/angular-query-experimental' | ||
import { lastValueFrom } from 'rxjs' | ||
import { PostsService } from '../services/posts-service' | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
selector: 'post', | ||
standalone: true, | ||
templateUrl: './post.component.html', | ||
imports: [RouterLink], | ||
}) | ||
export default class PostComponent { | ||
#postsService = inject(PostsService) | ||
|
||
postId = input<number, string>(0, { | ||
transform: (value: string) => Number(value), | ||
}) | ||
|
||
postQuery = injectQuery(() => ({ | ||
enabled: this.postId() > 0, | ||
queryKey: ['post', this.postId()], | ||
queryFn: async () => { | ||
return lastValueFrom(this.#postsService.postById$(this.postId())) | ||
}, | ||
})) | ||
} |
Oops, something went wrong.