Nuxt TypeScript - ChoDragon9/posts GitHub Wiki

๊ธ€์˜ ๋ชฉ์ 

Angular ๊ธฐ๋ฐ˜ํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•œ ๊ฒฝํ—˜์ด ์žˆ๋‹ค. Angular๋Š” TypeScript๋ฅผ ํ•„์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ ํ”„๋กœ์ ํŠธ์—์„œ TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์‚ฐ์„ฑ์ด ๋†’์•„์ง€๋Š” ๊ฒƒ์„ ๋ชธ์†Œ ๋Š๋ผ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ƒˆ๋กญ๊ฒŒ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๊ฒŒ๋˜๋ฉด TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋งˆ์Œ(?)๋จน์—ˆ๋‹ค.

์ด ๊ธ€์—์„œ๋Š” Nuxt๋ฅผ TypeScript ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€์ด๋“œํ•œ๋‹ค. Nuxt๋ฅผ TypeScript๋กœ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ์•„์ง ๋งŽ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์ž‘์„ฑํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” Nuxt์— TypeScript๋ฅผ ์„ธํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ TypeScript๋กœ ์ž‘์„ฑํ•œ Vue์™€ Vuex ์ฝ”๋“œ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค.

์„ค์น˜ ๋ฐ ์„ค์ •

๋จผ์ € ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ์žก๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•  ๋ชจ๋“ˆ ์„ค์น˜์™€ ํ™˜๊ฒฝ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

NPX ์„ค์น˜

TypeScript๋กœ ์ž‘์„ฑ๋œ Nuxt๋Š” npx๋ฅผ ํ†ตํ•ด์„œ ์ปค๋ฉ˜๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค. ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” dev ๋ช…๋ น์–ด๋Š” TypeScript๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹คํ–‰ํ•ด์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— npx ์„ค์น˜๋Š” ํ•„์ˆ˜์ด๋‹ค.

npx๋Š” node_modules/.bin์— ์„ค์น˜๋˜๋Š” ์ปค๋ฉ˜๋“œ๋ฅผ ํ”„๋กœ์ ํŠธ ๋กœ์ปฌ์—์„œ ์‹คํ–‰๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

$ npm install -g npx

Nuxt ํ”„๋กœ์ ํŠธ ์„ค์น˜

๊ณต์‹ ๊ฐ€์ด๋“œ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.

Nuxt๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์€ JS๋กœ ๊ฐœ๋ฐœํ•  ๋•Œ์™€ ๋™์ผํ•˜๋‹ค. ์ด ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด UI Framework์™€ Server-side Framework ์„ ํƒ์ด ๋‚˜์˜จ๋‹ค. ์›ํ•˜๋Š” Framework๋ฅผ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.

$ npx create-nuxt-app <project-name>

Nuxt TypeScript ์„ค์น˜

์ปดํŒŒ์ผ ํƒ€์ž„์— ํƒ€์ž… ์ฒดํฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ devDependencies์— @nuxt/typescript์™€ nuxt-property-decorator๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

$ npm install -D @nuxt/typescript nuxt-property-decorator

nuxt.config.ts์™€ serverMiddlewares์— TypeScript๋ฅผ ๋Ÿฐํƒ€์ž„์— ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด์„œ dependencies์— ts-node๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

$ npm install ts-node

์„ค์ • ํŒŒ์ผ ์ˆ˜์ •

๋ชจ๋“ˆ ์„ค์น˜๊ฐ€ ๋๋‚˜๋ฉด TypeScript๋กœ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

  1. touch tsconfig.json ๋ช…๋ น์–ด๋กœ TypeScript ์„ค์ •ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.
  2. npx nuxt ๋ช…๋ น์–ด๋กœ tsconfig.json์„ ์ˆ˜์ •ํ•œ๋‹ค. nuxt๋ฅผ ์ฒ˜์Œ ์‹คํ–‰ํ•˜๋ฉด tsconfig.json์„ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.
  3. tsconfig.json์— @nuxt/config์„ ์ถ”๊ฐ€ํ•œ๋‹ค. nuxt ํƒ€์ž…์„ IDE์—์„œ ์ž๋™์™„์„ฑ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋œ๋‹ค.
"types": [
 "@types/node",
 "@nuxt/vue-app",
+ "@nuxt/config"
]

์„ค์ •ํŒŒ์ผ์„ TypeScript๋กœ ๋ณ€๊ฒฝ

  1. nuxt.config.js๋ฅผ nuxt.config.ts๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
  2. TypeScript๋กœ nuxt.config.ts๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.
import NuxtConfiguration from '@nuxt/config'

const config: NuxtConfiguration = {
  // Type or Press `Ctrl + Space` for autocompletion
}

export default config

์‹คํ–‰ํ•˜๊ธฐ

Nuxt์—์„œ TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ช…๋ น์–ด๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. npm run dev ๋ช…๋ น์–ด๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์ง€๋งŒ ํ•ด๋‹น ๋ช…๋ น์–ด๋กœ๋Š” TypeScript๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

$ npx nuxt

๋นŒ๋“œํ•˜๊ธฐ

๋นŒ๋“œ๋Š” 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. SPA์™€ ์ •์ ํŒŒ์ผ ์ƒ์„ฑ์ด๋‹ค.

  • npx nuxt build: SPA ๋นŒ๋“œ
  • npx nuxt generate: ์ •์ ํŒŒ์ผ ์ƒ์„ฑ

๋นŒ๋“œ ํŒŒ์ผ ์‹คํ–‰ํ•˜๊ธฐ

๋นŒ๋“œ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋Š” dist ํด๋”๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

$ npx nuxt start

TypeScript๋กœ Vue ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

Vue์˜ <script>๋ฅผ TypeScript๋กœ ์„ค์ •ํ•˜๊ธฐ

๋จผ์ € ์ฃผ์˜ํ•  ์ ์€ .vueํŒŒ์ผ์— <script>๋Š” <script lang="ts">๋กœ๋งŒ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. lang="ts"๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด IDE์—์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž๋™์œผ๋กœ import๊ฐ€๋Šฅํ•˜๋‹ค.

์ปดํฌ๋„ŒํŠธ ์ž‘์„ฑํ•˜๊ธฐ

v-for๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด :key๋ฅผ ํ•„์ˆ˜๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

<template>
  <div class="VueToNuxtLogo">
    <template v-for="item in classes">
      <div :class="`${COMMON_CLASS} ${item}`" :key="item"></div>
    </template>
  </div>
</template>

<script lang="ts">
import Component from 'vue-class-component'
import { Vue } from 'nuxt-property-decorator'

@Component({})
class Logo extends Vue {
  COMMON_CLASS = 'Triangle'
  classes: string[] = [
    'Triangle--two',
    'Triangle--one',
    'Triangle--three',
    'Triangle--four'
  ]
}

export default Logo
</script>
<template>
  <div class="container">
    <logo />
    <a class="button--green" @click.prevent="upCount()">
      Up
    </a>
    { { count } }
    <a class="button--grey" @click.prevent="downCount()">
      Down
    </a>
  </div>
</template>

<script lang="ts">
import Component from 'vue-class-component'
import { Vue } from 'nuxt-property-decorator'
import Logo from '~/components/Logo.vue'

@Component({
  components: {
    Logo
  }
})
class Page extends Vue {
  count: number = 0
  upCount() {
    this.count++
  }
  downCount() {
    this.count++
  }
}

export default Page
</script>

Vuex ๋ฃจํŠธ์— ์‚ฌ์šฉํ•˜๊ธฐ

๋ฃจํŠธ์— ์ •์˜ํ•  ๋•Œ๋Š” store/index.ts์— state, mutations, getters, actions๋ฅผ ์ •์˜ํ•œ๋‹ค.

export const state = () => ({
  count: 0
})

export const mutations = {
  upCount(state) {
    state.count++
  },
  downCount(state) {
    state.count--
  }
}

์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋งŒ ๋ถ™์ด๋ฉด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

class Page extends Vue {
  @State count
  @Mutation upCount
  @Mutation downCount
}

Vuex ๋ชจ๋“ˆ ๋ชจ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

๋ชจ๋“ˆ ๋ชจ๋“œ๋กœ ์ •์˜ํ•  ๋•Œ๋Š” store์— ๋‹ค๋ฅธ ํŒŒ์ผ์„ ์ •์˜ํ•œ๋‹ค. ํŒŒ์ผ ๋‚ด๋ถ€์—๋Š” state, mutations, getters, actions๋ฅผ ์ •์˜ํ•œ๋‹ค.

// page.ts
export const state = () => ({
  count: 0
})

export const mutations = {
  upCount(state) {
    state.count++
  },
  downCount(state) {
    state.count--
  }
}

์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ชจ๋“ˆ์„ ์ ‘๊ทผํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

class Page extends Vue {
  @State((state) => state.page.count)
  count
  @Mutation('page/upCount')
  upCount
  @Mutation('page/downCount')
  downCount
}

๋

์ด ๊ธ€์—์„œ ์ž‘์„ฑ๋œ ์„ค์ •๊ณผ์ •์„ ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ๋กœ ๋งŒ๋“ค์–ด์„œ create-nuxt-ts์— ์˜ฌ๋ ธ๋‹ค. ์ด ์ €์žฅ์†Œ๋ฅผ ๋‚ด๋ ค๋ฐ›์œผ๋ฉด ์„ค์ • ๊ณผ์ •์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ ์ธํ…”๋ฆฌ์ œ์ด๋ฅผ ํ†ตํ•ด์„œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ธํŒ…์„ ํ–ˆ๋Š” ๋ฐ, ์ธํ…”๋ฆฌ์ œ์ด๋Š” IDE์— ์„ค์ •๊ณผ์ •์—์„œ ๋งŽ์€ ์‚ฝ์งˆ์ด ํ•„์š”ํ•˜๋‹ค. ์›น์Šคํ†ฐ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ„๋„๋กœ IDE ์„ค์ • ํ•„์š”์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ