Skip to content

Commit

Permalink
add pinia intro
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua1988 committed Dec 12, 2023
1 parent 6dec6cf commit b68b30f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
9 changes: 8 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ module.exports = {
children: ['/d3/d3', '/d3/vue-with-d3', '/d3/tutorial'],
},
{
title: 'State Management',
title: 'State Management - Vuex',
collapsable: false,
children: [
'/vuex/concept',
Expand All @@ -188,6 +188,13 @@ module.exports = {
'/vuex/state-vs-data',
],
},
{
title: 'State Management - Pinia ๐Ÿ†•',
collapsable: false,
children: [
'/pinia/intro',
],
},
{
title: 'Advanced Technics',
collapsable: false,
Expand Down
66 changes: 66 additions & 0 deletions docs/pinia/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: Introduction ๐Ÿ†•
---

# Pinia๋ž€?

ํ”ผ๋‹ˆ์•„(Pinia)๋ž€ Compositoin API ๊ธฐ๋ฐ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Vuex์˜ ์ตœ์‹  ๋ฒ„์ „์ธ 5๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

## Vuex๋ž‘ ๋ญ๊ฐ€ ๋‹ค๋ฅผ๊นŒ?

[๋ทฐ์—‘์Šค(Vuex)](/vuex/concept)๋Š” Vue.js์˜ ์ƒํƒœ ๊ด€๋ฆฌ ํŒจํ„ด์ด์ž ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ทฐ์—‘์Šค๊ฐ€ state, getters, mutations, actions๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒํƒœ๋ฅผ ๋‹ค๋ค˜๋‹ค๋ฉด ํ”ผ๋‹ˆ์•„๋Š” state, getters, actions๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ทฐ์—‘์Šค์˜ ๋‹ค์†Œ ๋ณต์žกํ•˜๊ฒŒ ๋ณด์ธ ๋ฌธ๋ฒ•๋“ค์ด ๋‹จ์ˆœํ•ด ์ง€๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์žฅ์ ๋งŒ ์žˆ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ํ”ผ๋‹ˆ์•„๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ปดํฌ์ง€์…˜ API ๊ธฐ๋ฐ˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ๋ทฐ์—‘์Šค๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ๋ฒ•์„ ์ตํ˜€์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํ”„๋กœ์ ํŠธ ์„ฑ๊ฒฉ์— ๋งž๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

:::tip
๐Ÿ‘‰ ์ด๋ฏธ ์šด์˜ ์ค‘์ธ Vue 2,3 ํ”„๋กœ์ ํŠธ๋Š” Vuex v4.x๋ฅผ ์‚ฌ์šฉ <br>
๐Ÿ‘‰ ์‹ ๊ทœ ํ”„๋กœ์ ํŠธ ์ค‘ **Composition API๋งŒ** ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋Š” Pinia๋ฅผ ์‚ฌ์šฉ
:::

## Pinia ์„ค์น˜

ํ”ผ๋‹ˆ์•„๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ Composition API๋กœ๋งŒ ์ž‘์„ฑ๋œ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Vue 2์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด [Composition API ํ”Œ๋Ÿฌ๊ทธ์ธ](https://github.com/vuejs/composition-api)์„ ๋ณ„๋„๋กœ ์„ค์น˜ํ•ด์•ผ ํ•˜๊ณ , Vue 3์—์„œ๋Š” ์ปดํฌ์ง€์…˜ API ์Šคํƒ€์ผ ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ปดํฌ์ง€์…˜ API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ทฐ ํ”„๋กœ์ ํŠธ์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ”ผ๋‹ˆ์•„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

```sh
# npm
npm install pinia
# yarn
yarn add pinia
```

## Pinia ๊ธฐ๋ณธ

ํ”ผ๋‹ˆ์•„๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” main.js ํŒŒ์ผ์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<code-group>
<code-block title="Vue 2">
```js
// main.js
import Vue from 'vue'
import { createPinia, PiniaVuePlugin } from 'pinia'
import App from './App.vue'

Vue.use(PiniaVuePlugin)
const pinia = createPinia()

new Vue({
render: h => h(App),
pinia,
}).$mount('#app');
```
</code-block>

<code-block title="Vue 3">
```js
// main.js
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const pinia = createPinia()
createApp(App).use(pinia).mount('#app')
```
</code-block>
</code-group>

0 comments on commit b68b30f

Please sign in to comment.