8000 feat: useXXX helpers (issue #1725) by lukeJEdwards · Pull Request #1954 · vuejs/vuex · GitHub
[go: up one dir, main page]

Skip to content

feat: useXXX helpers (issue #1725) #1954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
format: formatted the files
  • Loading branch information
Luke Edwards committed Mar 23, 2021
commit 8375b5a6945d3801747020cefe45fa4812c3fa04
78 changes: 39 additions & 39 deletions docs/api/index.md
10000
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ sidebar: auto
Creates a new store.

```js
import { createStore } from "vuex";
import { createStore } from 'vuex'

const store = createStore({ ...options });
const store = createStore({ ...options })
```

## Store Constructor Options
Expand Down Expand Up @@ -49,7 +49,7 @@ sidebar: auto
commit, // same as `store.commit`
dispatch, // same as `store.dispatch`
getters, // same as `store.getters`, or local getters if in modules
rootGetters; // same as `store.getters`, only in modules
rootGetters // same as `store.getters`, only in modules
}
```

Expand Down Expand Up @@ -130,7 +130,7 @@ sidebar: auto

```js
{
devtools: false;
devtools: false
}
```

Expand Down Expand Up @@ -186,18 +186,18 @@ Subscribe to store mutations. The `handler` is called after every mutation and r

```js
const unsubscribe = store.subscribe((mutation, state) => {
console.log(mutation.type);
console.log(mutation.payload);
});
console.log(mutation.type)
console.log(mutation.payload)
})

// you may call unsubscribe to stop the subscription
unsubscribe();
unsubscribe()
```

By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overridden by adding `prepend: true` to `options`, which will add the handler to the beginning of the chain.

```js
store.subscribe(handler, { prepend: true });
store.subscribe(handler, { prepend: true })
```

The `subscribe` method will return an `unsubscribe` function, which should be called when the subscription is no longer needed. For example, you might subscribe to a Vuex Module and unsubscribe when you unregister the module. Or you might call `subscribe` from inside a Vue Component and then destroy the component later. In these cases, you should remember to unsubscribe the subscription manually.
Expand All @@ -213,18 +213,18 @@ The `subscribe` method will return an `unsubscribe` function, which should be ca

```js
const unsubscribe = store.subscribeAction((action, state) => {
console.log(action.type);
console.log(action.payload);
});
console.log(action.type)
console.log(action.payload)
})

// you may call unsubscribe to stop the subscription
unsubscribe();
unsubscribe()
```

By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overridden by adding `prepend: true` to `options`, which will add the handler to the beginning of the chain.

```js
store.subscribeAction(handler, { prepend: true });
store.subscribeAction(handler, { prepend: true })
```

The `subscribeAction` method will return an `unsubscribe` function, which should be called when the subscription is no longer needed. For example, you might subscribe to a Vuex Module and unsubscribe when you unregister the module. Or you might call `subscribeAction` from inside a Vue Component and then destroy the component later. In these cases, you should remember to unsubscribe the subscription manually.
Expand All @@ -234,23 +234,23 @@ The `subscribeAction` method will return an `unsubscribe` function, which should
```js
store.subscribeAction({
before: (action, state) => {
console.log(`before action ${action.type}`);
console.log(`before action ${action.type}`)
},
after: (action, state) => {
console.log(`after action ${action.type}`);
},
});
console.log(`after action ${action.type}`)
}
})
```

`subscribeAction` can also specify an `error` handler to catch an error thrown when an action is dispatched. The function will receive an `error` object as the third argument.

```js
store.subscribeAction({
error: (action, state, error) => {
console.log(`error action ${action.type}`);
console.error(error);
},
});
console.log(`error action ${action.type}`)
console.error(error)
}
})
```

The `subscribeAction` method is most commonly used in plugins. [Details](../guide/plugins.md)
Expand Down Expand Up @@ -374,13 +374,13 @@ The second object argument's members can be a function. `function(commit: functi
Fetches the injected store when called inside the `setup` hook. When using the Composition API, you can retrieve the store by calling this method.

```js
import { useStore } from "vuex";
import { useStore } from 'vuex'

export default {
setup() {
const store = useStore();
},
};
const store = useStore()
}
}
```

TypeScript users can use an injection key to retrieve a typed store. In order for this to work, you must define the injection key and pass it along with the store when installing the store instance to the Vue app.
Expand All @@ -389,20 +389,20 @@ The second object argument's members can be a function. `function(commit: functi

```ts
// store.ts
import { InjectionKey } from "vue";
import { createStore, Store } from "vuex";
import { InjectionKey } from 'vue'
import { createStore, Store } from 'vuex'

export interface State {
count: number;
count: number
}

export const key: InjectionKey<Store<State>> = Symbol();
export const key: InjectionKey<Store<State>> = Symbol()

export const store = createStore<State>({
state: {
count: 0,
},
});
count: 0
}
})
```

Then, pass the defined key as the second argument for the `app.use` method.
Expand All @@ -423,14 +423,14 @@ The second object argument's members can be a function. `function(commit: functi

```ts
// in a vue component
import { useStore } from "vuex";
import { key } from "./store";
import { useStore } from 'vuex'
import { key } from './store'

export default {
setup() {
const store = useStore(key);
const store = useStore(key)

store.state.count; // typed as number
},
};
store.state.count // typed as number
}
}
```
56 changes: 28 additions & 28 deletions docs/guide/actions.md
9E7A
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ Let's register a simple action:
```js
const store = createStore({
state: {
count: 0,
count: 0
},
mutations: {
increment(state) {
state.count++;
},
state.count++
}
},
actions: {
increment(context) {
context.commit("increment");
},
},
});
context.commit('increment')
}
}
})
```

Action handlers receive a context object which exposes the same set of methods/properties on the store instance, so you can call `context.commit` to commit a mutation, or access the state and getters via `context.state` and `context.getters`. We can even call other actions with `context.dispatch`. We will see why this context object is not the store instance itself when we introduce [Modules](modules.md) later.
Expand All @@ -44,7 +44,7 @@ actions: {
Actions are triggered with the `store.dispatch` method:

```js
store.dispatch("increment");
store.dispatch('increment')
```

This may look silly at first sight: if we want to increment the count, why don't we just call `store.commit('increment')` directly? Remember that **mutations have to be synchronous**. Actions don't. We can perform **asynchronous** operations inside an action:
Expand All @@ -63,15 +63,15 @@ Actions support the same payload format and object-style dispatch:

```js
// dispatch with a payload
store.dispatch("incrementAsync", {
amount: 10,
});
store.dispatch('incrementAsync', {
amount: 10
})

// dispatch with an object
store.dispatch({
type: "incrementAsync",
amount: 10,
});
type: 'incrementAsync',
amount: 10
})
```

A more practical example of real-world actions would be an action to checkout a shopping cart, which involves **calling an async API** and **committing multiple mutations**:
Expand Down Expand Up @@ -103,22 +103,22 @@ Note we are performing a flow of asynchronous operations, and recording the side
You can dispatch actions in components with `this.$store.dispatch('xxx')`, or use the `mapActions` helper which maps component methods to `store.dispatch` calls (requires root `store` injection):

```js
import { mapActions } from "vuex";
import { mapActions } from 'vuex'

export default {
// ...
methods: {
...mapActions([
"increment", // map `this.increment()` to `this.$store.dispatch('increment')`
'increment', // map `this.increment()` to `this.$store.dispatch('increment')`

// `mapActions` also supports payloads:
"incrementBy", // map `this.incrementBy(amount)` to `this.$store.dispatch('incrementBy', amount)`
'incrementBy' // map `this.incrementBy(amount)` to `this.$store.dispatch('incrementBy', amount)`
]),
...mapActions({
add: "increment", // map `this.add()` to `this.$store.dispatch('increment')`
}),
},
};
add: 'increment' // map `this.add()` to `this.$store.dispatch('increment')`
})
}
}
```

## Composing Actions
Expand All @@ -143,9 +143,9 @@ actions: {
Now you can do:

```js
store.dispatch("actionA").then(() => {
store.dispatch('actionA').then(() => {
// ...
});
})
```

And also in another action:
Expand Down Expand Up @@ -200,15 +200,15 @@ export default {
or object destructuring

```js
import { useActions } from "vuex";
import { useActions } from 'vuex'

export default {
setup() {
return {
...useActions(["increment"]),
};
},
};
...useActions(['increment'])
}
}
}
```

You can learn more in the [Composition API](./composition-api#New-helper-methods-for-Composition-API) section.
Loading
0