-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from GuoXiCheng/dev-c
update docs
- Loading branch information
Showing
14 changed files
with
106 additions
and
23 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 开始使用 | ||
|
||
## 安装 pinia | ||
|
||
```bash | ||
npm install pinia | ||
``` | ||
|
||
## 创建 pinia 实例 | ||
|
||
::: code-group | ||
|
||
```js [Vue 3] | ||
import { createApp } from "vue"; | ||
import { createPinia } from "pinia"; | ||
import App from "./App.vue"; | ||
|
||
const pinia = createPinia(); | ||
const app = createApp(App); | ||
|
||
app.use(pinia); | ||
app.mount("#app"); | ||
``` | ||
|
||
```js [Vue 2] | ||
import { createPinia, PiniaVuePlugin } from "pinia"; | ||
|
||
Vue.use(PiniaVuePlugin); | ||
const pinia = createPinia(); | ||
|
||
new Vue({ | ||
el: "#app", | ||
// 其他配置... | ||
// ... | ||
// 请注意,同一个`pinia'实例 | ||
// 可以在同一个页面的多个 Vue 应用中使用。 | ||
pinia, | ||
}); | ||
``` | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# pinia 介绍 | ||
|
||
## 什么是 pinia | ||
|
||
Pinia 是一个拥有组合式 API 的 Vue 状态管理库。 | ||
|
||
## 完整的 Pinia API 示例 | ||
|
||
```js | ||
import { defineStore } from "pinia"; | ||
|
||
export const useTodos = defineStore("todos", { | ||
state: () => ({ | ||
/** @type {{ text: string, id: number, isFinished: boolean }[]} */ | ||
todos: [], | ||
/** @type {'all' | 'finished' | 'unfinished'} */ | ||
filter: "all", | ||
// 类型将自动推断为 number | ||
nextId: 0, | ||
}), | ||
getters: { | ||
finishedTodos(state) { | ||
// 自动补全! ✨ | ||
return state.todos.filter((todo) => todo.isFinished); | ||
}, | ||
unfinishedTodos(state) { | ||
return state.todos.filter((todo) => !todo.isFinished); | ||
}, | ||
/** | ||
* @returns {{ text: string, id: number, isFinished: boolean }[]} | ||
*/ | ||
filteredTodos(state) { | ||
if (this.filter === "finished") { | ||
// 调用其他带有自动补全的 getters ✨ | ||
return this.finishedTodos; | ||
} else if (this.filter === "unfinished") { | ||
return this.unfinishedTodos; | ||
} | ||
return this.todos; | ||
}, | ||
}, | ||
actions: { | ||
// 接受任何数量的参数,返回一个 Promise 或不返回 | ||
addTodo(text) { | ||
// 你可以直接变更该状态 | ||
this.todos.push({ text, id: this.nextId++, isFinished: false }); | ||
}, | ||
}, | ||
}); | ||
``` |