What You’ll Build
We’ll create a simple blog post viewer with:- Fetching and displaying a list of posts
- Loading and error states
- Creating new posts with mutations
- Automatic cache updates
Prerequisites
Make sure you’ve completed the installation steps:- Installed
@tanstack/angular-query-experimental - Configured
provideTanStackQueryin your app
Step 1: Create a Data Service
First, let’s create a service to fetch data. We’ll use Angular’sHttpClient:
Step 2: Fetch and Display Posts
Now let’s create a component that fetches and displays posts:components/posts-list.component.ts
Understanding the Code
- Query Setup
- Query State
- Template
queryKey: A unique identifier for this query. Used for caching and refetching.queryFn: An async function that fetches the data.injectQuery: Returns a signal-based object with query state and data.
Step 3: Add a Single Post View
Let’s create a component to view a single post with reactive parameters:components/post-detail.component.ts
Step 4: Create Posts with Mutations
Now let’s add the ability to create new posts:components/create-post.component.ts
Understanding Mutations
- Mutation Setup
- Mutation State
- Cache Updates
mutationFn: The function that performs the mutationonSuccess: Callback called when mutation succeedsinvalidateQueries: Marks queries as stale to trigger refetch
Step 5: Put It All Together
Create a main app component that uses all the pieces:app.component.ts
Next Steps
Queries Guide
Learn advanced query patterns
Mutations Guide
Master mutations and optimistic updates
DevTools
Debug your queries visually
API Reference
Explore the complete API