You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`--path`| Path to the module that you wish to add the import for the `provideRedux` to. |`string`||
30
+
|`--project`| Name of the project defined in your `angular.json` to help locating the module to add the `provideRedux` to. |`string`||
31
31
|`--module`| Name of file containing the module that you wish to add the import for the `provideRedux` to. Can also include the relative path to the file. For example, `src/app/app.module.ts`. |`string`|`app`|
32
32
|`--storePath`| The file path to create the state in. |`string`|`store`|
You'll also need to [install Redux](https://redux.js.org/introduction/installation) and [set up a Redux store](https://redux.js.org/recipes/configuring-your-store/) in your app.
55
54
56
55
Angular-Redux is written in TypeScript, so all types are automatically included.
@@ -62,15 +61,13 @@ Angular-Redux is written in TypeScript, so all types are automatically included.
62
61
Angular Redux includes a `provideRedux` provider factory, which makes the Redux store available to the rest of your app:
@@ -81,29 +78,25 @@ Angular Redux provides a pair of custom Angular injectable functions that allow
81
78
`injectSelector` reads a value from the store state and subscribes to updates, while `injectDispatch` returns the store's `dispatch` method to let you dispatch actions.
Create a file named `src/app/store.ts`. Import the `configureStore` API from Redux Toolkit. We'll start by creating an empty Redux store, and exporting it:
49
49
50
50
```typescript title="app/store.ts"
51
-
import { configureStore } from'@reduxjs/toolkit'
51
+
import { configureStore } from"@reduxjs/toolkit";
52
52
53
53
exportdefaultconfigureStore({
54
54
reducer: {},
55
-
})
55
+
});
56
56
```
57
57
58
58
This creates a Redux store, and also automatically configure the Redux DevTools extension so that you can inspect the store while developing.
@@ -62,19 +62,18 @@ This creates a Redux store, and also automatically configure the Redux DevTools
62
62
Once the store is created, we can make it available to our Angular components by putting an Angular Redux `provideRedux` in our application's `providers` array in `src/main.ts`. Import the Redux store we just created, put a `provideRedux` in your application's `providers` array, and pass the store as a prop:
@@ -87,10 +86,10 @@ Creating a slice requires a string name to identify the slice, an initial state
87
86
Redux requires that [we write all state updates immutably, by making copies of data and updating the copies](https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow#immutability). However, Redux Toolkit's `createSlice` and `createReducer` APIs use [Immer](https://immerjs.github.io/immer/) inside to allow us to [write "mutating" update logic that becomes correct immutable updates](https://redux.js.org/tutorials/fundamentals/part-8-modern-redux#immutable-updates-with-immer).
Next, we need to import the reducer function from the counter slice and add it to our store. By defining a field inside the `reducer` parameter, we tell the store to use this slice reducer function to handle all updates to that state.
### Use Redux State and Actions in Angular Components
139
138
140
139
Now we can use the Angular Redux inject functions to let Angular components interact with the Redux store. We can read data from the store with `useSelector`, and dispatch actions using `useDispatch`. Create a `src/features/counter/counter.component.ts` file with a `<app-counter>` component inside, then import that component into `app.component.ts` and render it inside of `<app-root>`.
0 commit comments