Skip to content

๐Ÿ ํ•œ๊ธ€ํ™” ์ž‘์—…์„ ์œ„ํ•ด Vue.js์˜ ๊ณต์‹ Pinia๋ฅผ ํฌํฌํ•œ ์ €์žฅ์†Œ ์ž…๋‹ˆ๋‹ค.

License

Notifications You must be signed in to change notification settings

niceplugin/Vuejs-Pinia-KO

ย 
ย 

Repository files navigation

Pinia logo


npm package build status


Pinia

์ง๊ด€์ ์ด๊ณ  ์œ ์—ฐํ•œ Vue.js ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • ๐Ÿ’ก ์ง๊ด€์ 
  • ๐Ÿ”‘ Type-safe
  • โš™๏ธ ๊ฐœ๋ฐœ ๋„๊ตฌ ์ง€์›
  • ๐Ÿ”Œ ํ™•์žฅ ๊ฐ€๋Šฅ
  • ๐Ÿ— ๋ชจ๋“ˆ์‹ ์„ค๊ณ„
  • ๐Ÿ“ฆ ๊ทน๋„๋กœ ๊ฐ€๋ฒผ์›€
  • โ›ฐ๏ธ Nuxt ๋ชจ๋“ˆ

Pinia๋Š” Vue 2์™€ Vue 3 ๋ชจ๋‘์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

Pinia๋Š” ์ŠคํŽ˜์ธ์–ด๋กœ 'ํŒŒ์ธ์• ํ”Œ'์ด๋ผ๋Š” ๋‹จ์–ด์˜ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์˜์–ด ๋ฐœ์Œ์ž…๋‹ˆ๋‹ค: piรฑa. ์‹ค์ œ๋กœ ํŒŒ์ธ์• ํ”Œ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐœ๋ณ„ ๊ฝƒ๋“ค์ด ๋ชจ์—ฌ ํ•˜๋‚˜์˜ ๋‹ค์ค‘ ๊ณผ์ผ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์Šคํ† ์–ด๋“ค์ฒ˜๋Ÿผ, ๊ฐ๊ฐ์€ ๊ฐœ๋ณ„์ ์œผ๋กœ ํƒ„์ƒํ•˜์ง€๋งŒ, ๊ฒฐ๊ตญ ๋ชจ๋‘ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‚จ์•„๋ฉ”๋ฆฌ์นด ์›์‚ฐ์˜ ๋ง›์žˆ๋Š” ์—ด๋Œ€ ๊ณผ์ผ์ž…๋‹ˆ๋‹ค.

์ด ํ”„๋กœ์ ํŠธ๋ฅผ ๊ณ„์† ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ์„ธ์š” ๐Ÿ’š


FAQ

์ด ํ”„๋กœ์ ํŠธ์™€ ๊ฐ€๋Šฅํ•œ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ๋…ธํŠธ:

Q: Pinia๋Š” Vuex์˜ ํ›„์†์ž‘์ธ๊ฐ€์š”?

A: ์˜ˆ

Q: ๋™์  ๋ชจ๋“ˆ์€ ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?

A: ๋™์  ๋ชจ๋“ˆ์€ ํƒ€์ž… ์•ˆ์ „ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ๋Œ€์‹  ๋‹ค๋ฅธ ์Šคํ† ์–ด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค ๊ทธ๋ฆฌ๊ณ  ์–ด๋””์„œ๋‚˜ ์ž„ํฌํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

์„ค์น˜

# ๋˜๋Š” pnpm ๋˜๋Š” yarn
npm install pinia

Vue <2.7์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์ตœ์‹  @vue/composition-api๋ฅผ ์„ค์น˜ํ•˜์„ธ์š”:

npm install pinia @vue/composition-api

์‚ฌ์šฉ๋ฒ•

ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

pinia(๋ฃจํŠธ store)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•ฑ์— ์ „๋‹ฌํ•˜์„ธ์š”:

// 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')
// Vue 2
import { createPinia, PiniaVuePlugin } from 'pinia'

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

new Vue({
  el: '#app',
  // ๊ธฐํƒ€ ์˜ต์…˜...
  // ...
  // ํŽ˜์ด์ง€ ๋‚ด ์—ฌ๋Ÿฌ Vue ์•ฑ์—์„œ ๋™์ผํ•œ `pinia` ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  pinia,
})

Nuxt ๊ตฌ์„ฑ์„ ํฌํ•จํ•œ ๋” ์ž์„ธํ•œ ์ง€์นจ์€ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

Store ์ƒ์„ฑ

You can create as many stores as you want, and they should each exist in different files: ์›ํ•˜๋Š” ๋งŒํผ store๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ store๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํŒŒ์ผ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

import { defineStore } from 'pinia'

// main is the name of the store. It is unique across your application
// and will appear in devtools

// main์€ ์Šคํ† ์–ด์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.
// ์ด ์ด๋ฆ„์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ๊ณ ์œ ํ•˜๋ฉฐ devtools์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.
export const useMainStore = defineStore('main', {
  // ํ•„์ˆ˜: ์ƒˆ๋กœ์šด ์ƒํƒœ(state)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  state: () => ({
    counter: 0,
    name: 'Eduardo',
  }),
  // ์„ ํƒ: getters
  getters: {
    // getter๋“ค์€ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ state๋ฅผ ์ „๋‹ฌ ๋ฐ›์Šต๋‹ˆ๋‹ค.
    doubleCounter: (state) => state.counter * 2,
    // ๋‹ค๋ฅธ getter์—์„œ getter ์‚ฌ์šฉ
    doubleCounterPlusOne(): number {
      return this.doubleCounter + 1
    },
  },
  // ์„ ํƒ: actions
  actions: {
    reset() {
      // `this`๋Š” store ์ธ์Šคํ„ด์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      this.counter = 0
    },
  },
})

defineStore๋Š” store์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

import { useMainStore } from '@/stores/main'
import { storeToRefs } from 'pinia'

export default defineComponent({
  setup() {
    const main = useMainStore()

    // ํŠน์ • store ์†์„ฑ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
    const { counter, doubleCounter } = storeToRefs(main)

    return {
      // ํ…œํ”Œ๋ฆฟ์—์„œ main์ด๋ผ๋Š” store ์ „์ฒด๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
      main,
      // ํŠน์ • state ๋˜๋Š” getter๋งŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
      counter,
      doubleCounter,
    }
  },
})

๋ฌธ์„œ

Pinia์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๋ ค๋ฉด ํ•œ๊ธ€ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

๋ผ์ด์„ผ์Šค

MIT

About

๐Ÿ ํ•œ๊ธ€ํ™” ์ž‘์—…์„ ์œ„ํ•ด Vue.js์˜ ๊ณต์‹ Pinia๋ฅผ ํฌํฌํ•œ ์ €์žฅ์†Œ ์ž…๋‹ˆ๋‹ค.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 74.6%
  • Vue 16.3%
  • JavaScript 6.3%
  • CSS 2.1%
  • Other 0.7%