惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
J
Java Code Geeks
L
LangChain Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
B
Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog

Nx Blog

Sharing Tailwind CSS Styles Across Apps in a Monorepo | Nx Blog How SiriusXM Stays Competitive by Iterating and Getting to Market Fast | Nx Blog Agentic Experience Is the New Developer Experience | Nx Blog Nx Joins the Linux Foundation and the Agentic AI Foundation | Nx Blog A Monorepo Is NOT a Monolith | Nx Blog Why we deleted (most of) our MCP tools | Nx Blog Teach Your AI Agent How to Work in a Monorepo | Nx Blog How Broadcom stays efficient and nimble with monorepos | Nx Blog Why Monorepos are King in the Age of AI | Nx Blog Nx 2026 Roadmap: Expanding Agent Autonomy, Improving Performance, Better Polyglot and More | Nx Blog End to End Autonomous AI Agent Workflows with Nx | Nx Blog Autonomous Agents at Scale | Nx Blog Scaling 700+ Projects: How Nx Became a 'No-Brainer' for Caseware | Nx Blog Configure Tailwind v4 with Angular in an Nx Monorepo | Nx Blog The Missing Multiplier for AI Agent Productivity | Nx Blog A Year of Nx Webinars | Nx Blog Wrapping Up 2025 | Nx Blog Nx 22.3 Release: Angular 21 Support, tsgo Compiler, and Prettier v3 | Nx Blog Nx Cloud Release: Agent Resource Usage | Nx Blog Nx Platform Outperforms DIY Cache by 5x | Nx Blog An Nx Carol: Past, Present, and Future of Your Monorepo | Nx Blog Nx 22.1 Release: Terminal UI on Windows, Storybook 10, Vitest 4, and more! | Nx Blog The Compounding Effect: How Nx Features Multiply Performance Gains | Nx Blog 10 Monorepo Myths Debunked: Separating Fact from Fiction | Nx Blog Nx Cloud Release: Enterprise Task Analytics | Nx Blog Watch and Rebuild Storybook Dependencies with Nx | Nx Blog Book - React for Enterprise: Timeless Architecture for Enterprise Apps | Nx Blog Beyond Remote Cache: Unlock 70% More CI Performance | Nx Blog Nx 22 Release: Expanding the build platform | Nx Blog What's the Point of Generating All This Code If You Can't Merge It? | Nx Blog
Using NgRx Standalone APIs with Nx | Nx Blog
Colum Ferry · 2023-02-22 · via Nx Blog

Version 15 of NgRx introduced Standalone APIs to the package, enabling usage of the NgRx with Standalone Component-based Angular applications. This allows for a simpler integration of NgRx to your application.

Nx has added support for using these Standalone APIs from NgRx when generating NgRx stores with our @nrwl/angular:ngrx generator when you give it a path to a Routes definition file. (Usually denoted by _*.routes.ts_)

In this article, we'll walk through using Nx to create a new Standalone Component-based Angular application and add NgRx to it, using ONLY Nx Generators!

Prefer a video version? We've got you covered!

Create a new Nx Workspace

npx create-nx-workspace myorg

Select:

  • Standalone Angular app
  • Yes to using Standalone Components
  • Yes to add routing
  • Any option for the stylesheet format
  • Yes to Nx Cloud

The result should look something like this:

Now run cd myorg to enter the workspace.

The src/main.ts should look different than you remember with standard NgModule-based Angular applications.

You should see something similar to

import { bootstrapApplication } from '@angular/platform-browser';
import {
  provideRouter,
  withEnabledBlockingInitialNavigation,
} from '@angular/router';
import { AppComponent } from './app/app.component';
import { appRoutes } from './app/app.routes';

bootstrapApplication(AppComponent, {
  providers: \[provideRouter(appRoutes, withEnabledBlockingInitialNavigation())\],
}).catch((err) => console.error(err));

This is important, as this is the file where your root NgRx providers need to be placed, within the providers option of the bootstrapApplication function.

Nx will aid this also!

Generate the root state

nx g @nrwl/angular:ngrx --root --parent=src/main.ts

You'll be asked for a name for the feature state, but you can ignore this and simply press enter in your terminal, it is not necessary at this stage.

Say false to Facades also.

The generator will now make changes to your main.ts file and install the NgRx packages for you!

Your main.ts file should now look like this

import { bootstrapApplication } from '@angular/platform-browser';
import {
  provideRouter,
  withEnabledBlockingInitialNavigation,
} from '@angular/router';
import { AppComponent } from './app/app.component';
import { appRoutes } from './app/app.routes';
import { provideStore, provideState } from '@ngrx/store';
import { provideEffects } from '@ngrx/effects';

bootstrapApplication(AppComponent, {
  providers: \[
    provideEffects(),
    provideStore(),
    provideRouter(appRoutes, withEnabledBlockingInitialNavigation()),
  \],
}).catch((err) => console.error(err));

Notice the addition of provideEffects() and provideStore() to the providers array.

Generate a new feature library

NgRx works better when you split your store based on the features you have within your application. This is a pretty common use case. Nx allows you to do this very easily and in a very structured way.

First, let's generate a new feature library, called feature-users, in Nx that will house everything related to our feature including the NgRx State.

nx g @nrwl/angular:lib feature-users --standalone --routing --lazy --parent=src/app/app.routes.ts

This command does a few things:

  • It creates a new library in our Nx Workspace
  • It uses an Angular Standalone Component as the entrypoint
  • It adds a routing configuration to the library and adds the component as the default route.
  • It will add a lazy-loaded route to the application's app.routes.ts file, wiring up the application to the library!

Some files you may want to explore in your own time are:

src/app/app.routes.ts
feature-users/src/lib/lib.routes.ts

Add feature state to the feature library

Now that we have a feature library for our users feature, let's generate the feature state! It's as simple as one command.

nx g @nrwl/angular:ngrx users --parent=feature-users/src/lib/lib.routes.ts --route=''

You'll be asked if this is the root state of the application, enter N. Then say no to Facades (unless you really want them).

The --route option here is used to dictate what route within our routes definition file (lib.routes.ts) should have the state attached to it. This is to allow the NgRx Standalone APIs to be attached to that route.

We can see that if we look at the lib.routes.ts file

import { Route } from '@angular/router';
import { FeatureUsersComponent } from './feature-users/feature-users.component';
import { provideStore, provideState } from '@ngrx/store';
import { provideEffects } from '@ngrx/effects';
import \* as fromUsers from './+state/users.reducer';
import { UsersEffects } from './+state/users.effects';

export const featureUsersRoutes: Route\[\] = \[
  {
    path: '',
    component: FeatureUsersComponent,
    providers: \[
      provideState(fromUsers.USERS\_FEATURE\_KEY, fromUsers.usersReducer),
      provideEffects(UsersEffects),
    \],
  },
\];

The command will also have generated our

  • Actions
  • Reducers
  • Selectors
  • Effects

And with that, we now have NgRx installed and integrated into our application!

If you'd like to confirm the integration, you can run the following commands and see successful outputs!

nx build

nx test

nx test feature-users

Conclusion

This guide has shown how easy it is to set up NgRx with Nx and how to take advantage of the latest Standalone APIs with your application. All with just 5 Nx commands!

With the Angular roadmap pointing to Standalone Components becoming the preferred option for developing Angular applications, this support will be crucial in the future, and Nx will help you achieve it with the best possible DX!

Btw, did you know you can now scaffold a single-project Angular Nx workspace that fully leverages the Standalone APIs? Check it out here: https://youtu.be/Hi3aJ0Rlkls

You can check out an example repository that was created following the steps above here: https://github.com/Coly010/nx-ngrx-standalone

Learn More

🧠 Nx Docs
👩‍💻 Nx GitHub
💬 Nx Official Discord Server 📹 Nrwl Youtube Channel
🥚 Free Egghead course
🧐 Need help with Angular, React, Monorepos, Lerna or Nx? Talk to us 😃