Skip to content

Commit

Permalink
docs(angular-query): add routing example (#6873)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoud-dv committed Feb 11, 2024
1 parent 59c0d53 commit 3b6c32a
Show file tree
Hide file tree
Showing 43 changed files with 914 additions and 287 deletions.
22 changes: 11 additions & 11 deletions examples/angular/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^17.1.2",
"@angular/common": "^17.1.2",
"@angular/compiler": "^17.1.2",
"@angular/core": "^17.1.2",
"@angular/forms": "^17.1.2",
"@angular/platform-browser": "^17.1.2",
"@angular/platform-browser-dynamic": "^17.1.2",
"@angular/router": "^17.1.2",
"@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.2",
"@angular/cli": "^17.1.2",
"@angular/compiler-cli": "^17.1.2",
"@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"
}
Expand Down
8 changes: 4 additions & 4 deletions examples/angular/default-query-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"test:types": "tsc"
},
"dependencies": {
"@angular/common": "^17.1.2",
"@angular/compiler": "^17.1.2",
"@angular/core": "^17.1.2",
"@angular/platform-browser": "^17.1.2",
"@angular/common": "^17.1.3",
"@angular/compiler": "^17.1.3",
"@angular/core": "^17.1.3",
"@angular/platform-browser": "^17.1.3",
"@tanstack/angular-query-devtools-experimental": "^5.20.1",
"@tanstack/angular-query-experimental": "^5.20.1",
"axios": "^1.6.7",
Expand Down
6 changes: 6 additions & 0 deletions examples/angular/infinite-query-with-max-pages/.eslintrc.cjs
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
22 changes: 11 additions & 11 deletions examples/angular/infinite-query-with-max-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^17.1.2",
"@angular/common": "^17.1.2",
"@angular/compiler": "^17.1.2",
"@angular/core": "^17.1.2",
"@angular/forms": "^17.1.2",
"@angular/platform-browser": "^17.1.2",
"@angular/platform-browser-dynamic": "^17.1.2",
"@angular/router": "^17.1.2",
"@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.2",
"@angular/cli": "^17.1.2",
"@angular/compiler-cli": "^17.1.2",
"@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"
}
Expand Down
4 changes: 4 additions & 0 deletions examples/angular/router/.devcontainer/devcontainer.json
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"
}
6 changes: 6 additions & 0 deletions examples/angular/router/.eslintrc.cjs
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
6 changes: 6 additions & 0 deletions examples/angular/router/README.md
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`
100 changes: 100 additions & 0 deletions examples/angular/router/angular.json
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"
}
}
}
}
}
}
32 changes: 32 additions & 0 deletions examples/angular/router/package.json
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"
}
}
12 changes: 12 additions & 0 deletions examples/angular/router/src/app/app.component.html
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 />
12 changes: 12 additions & 0 deletions examples/angular/router/src/app/app.component.ts
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 {}
17 changes: 17 additions & 0 deletions examples/angular/router/src/app/app.config.ts
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()),
],
}
15 changes: 15 additions & 0 deletions examples/angular/router/src/app/app.routes.ts
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 examples/angular/router/src/app/components/post.component.html
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 examples/angular/router/src/app/components/post.component.ts
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()))
},
}))
}

0 comments on commit 3b6c32a

Please sign in to comment.