Vue3 has been released for some time, it adopts the new Responsive system, and built a new Composition API
. Vue's surrounding ecosystem is stepping up its efforts to adapt to this new system, and the official state management library Vuex is also adapting. For this reason, the official has put forward a new proposal for Vuex 5. [Related recommendations: "vue.js Tutorial"]
- supports two syntaxes for creating Store:
Options Api
andComposition Api
; - delete
mutations
, only supportstate
,getters
,actions
; - Modular design can well support code splitting;
- There are no nested modules, only the concept of Store;
- Complete
TypeScript
Support;
Below this proposal, there is an interesting comment. A brief translation:
What a coincidence, the Vuex5 proposal cannot be said to have nothing to do with the functions implemented by Pinia, it can only be said to be exactly the same. Today’s article will give you Let me introduce this pineapple.
Installation
In the existing project, the following command is used to install the Pinia module.
# yarn yarn add pinia@next # npm npm i pinia@next
After the installation is completed, you need to import and install it in the entry file of the Vue3 project.
// main.js import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' // 实例化 Vue const app = createApp(App) // 安装 Pinia app.use(createPinia()) // 挂载在真实 DOM app.mount('#app')
Getting Started
To use Pinia, you only need to define a store and then import it where the data is used.
Define Store
import { defineStore } from "pinia" // 对外部暴露一个 use 方法,该方法会导出我们定义的 state const useCounterStore = defineStore({ // 每个 store 的 id 必须唯一 id: 'counter', // state 表示数据源 state: () => ({ count: 0 }), // getters 类似于 computed,可对 state 的值进行二次计算 getters: { double () { // getter 中的 this 指向 state return this.count * 2 }, // 如果使用箭头函数会导致 this 指向有问题 // 可以在函数的第一个参数中拿到 state double: (state) => { return state.count * 2 } }, // actions 用来修改 state actions: { increment() { // action 中的 this 指向 state this.count++ }, } }) export default useCounterStore
In addition to using the above vuex-like method to build state, you can also use the function
form to create a store, which is somewhat similar. setup()
in Vue3.
import { ref, computed } from "vue" import { defineStore } from "pinia" // 对外部暴露一个 use 方法,该方法会导出我们定义的 state const useCounterStore = defineStore('counter', function () { const count = ref(0) const double = computed(() => count.value * 2) function increment() { count.value++ } return { count, double, increment } }) export default useCounterStore
Using Store
As mentioned before, Pinia provides two ways to use store, Options Api
and Composition Api
Zhongdu perfectly supports it.
Options Api
In Options Api
, you can directly use the officially provided mapActions
and mapState
method, export the state, getter, and action in the store. Its usage is basically the same as Vuex, and it is easy to get started.
import { mapActions, mapState } from 'pinia' import { useCounterStore } from '../model/counter' export default { name: 'HelloWorld', computed: { ...mapState(useCounterStore, ['count', 'double']) }, methods: { ...mapActions(useCounterStore, ['increment']) } }
Composition Api
Composition Api
, both state and getter need to listen for changes through the computed
method , which is the same as Options Api
, which needs to be placed in the computed
object. In addition, the state value obtained in Options Api
can be modified directly. Of course, it is recommended to write an action to operate the state value to facilitate later maintenance.
// Composition Api import { computed } from 'vue' import { useCounterStore } from '../stores/counter' export default { name: 'HelloWorld', setup() { const counter = useCounterStore() return { // state 和 getter 都需要在使用 computed,这和 Options Api 一样 count: computed(() => counter.count), double: computed(() => counter.double), increment: () => { counter.count++ }, // 可以直接修改 state 的值 increment: counter.increment, // 可以引用 store 中定义的 action } } }
Type hints
In Vuex, TypeScript's type hints are not very good. When performing type derivation, only its state can be found. Especially in the process of writing code, the code prompts are very unintelligent.
And pinia can deduce all defined states, getters, and actions, which will be much more convenient when writing code.
Mainly because pinia has a very friendly type definition through TypeScript. If you are interested, you can take a look at pinia’s type definition file (pinia.d.ts
):
Code split
Due to the use of modular design, all Stores can be introduced separately, instead of mounting all modules to one store through modules like vuex.
Assume that we currently create a Store through Vuex. There are two modules under this Store, namely the user module (User) and the product module (Goods). Even if the current homepage only uses user information, the entire Store will be packaged into the js chunk of the homepage.
If we use pinia, we will use defineStore
to define two stores that are completely separate. When the two pages are introduced, they will not affect each other. During the final packaging, the js chunk of the homepage and the js chunk of the product page will be packaged into the corresponding stores respectively.
The introduction of Pinia comes to an end here. If you have a new project to be developed using Vue3, it is recommended to use Pinia without any brain. It is more concise and only 1KB in size.
For more programming-related knowledge, please visit: Introduction to Programming! !