๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

"nuxt" ํƒœ๊ทธ๋กœ ์—ฐ๊ฒฐ๋œ 3๊ฐœ ๊ฒŒ์‹œ๋ฌผ๊ฐœ์˜ ๊ฒŒ์‹œ๋ฌผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  ํƒœ๊ทธ ๋ณด๊ธฐ

ยท ์•ฝ 4๋ถ„

Nuxt.js์˜ View๋Š” app template๊ณผ Layout, Page๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. SEO ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ดํŽ˜์ด์ง€ ๋ณ„๋กœ meta ํƒœ๊ทธ์™€ head ์„น์…˜์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Composition of a View in Nuxt.js

Pages#

๋ชจ๋“  ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ทฐ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค. ํ•˜์ง€๋งŒ Nuxt.js๋Š” ์‰ฝ๊ฒŒ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋กํŠน๋ณ„ํ•œ ์†์„ฑ๋“ค๊ณผ ํ•จ์ˆ˜๋“ค์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋ชจ๋“  ํŽ˜์ด์ง€ ์†์„ฑ์„ ๋ณด๋ ค๋ฉด Directory Structure๋ฅผ์ฐธ๊ณ 

Layouts#

๋ ˆ์ด์•„์›ƒ์€ Nuxt.js ์•ฑ์˜ Look & Feel์„ ๋ณ€๊ฒฝํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ด๋“œ๋ฐ”)

๋ ˆ์ด์•„์›ƒ์€ layouts ๋””๋ ‰ํ† ๋ฆฌ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

default.vue ํŒŒ์ผ์€ ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ์„ ์ •์˜ํ•œ๋‹ค.

layouts/default.vue
<template>    <Nuxt /></template>

๋ ˆ์ด์•„์›ƒ์—๋Š” <Nuxt /> ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ ์™ธ์˜ ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ์€ layouts ํด๋”์— ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

layouts/blog.vue
<template>    <div>        <div>My blog navigation bar here</div>        <Nuxt />    </div></template>

์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, page ์ปดํฌ๋„ŒํŠธ์—์„œ layout ์†์„ฑ์— ํŒŒ์ผ ์ด๋ฆ„์„ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

pages/posts.vue
<template>    <!-- Your template --></template><script>    export default {        layout: 'blog',        // page component definitions    };</script>

Error Page#

์—๋Ÿฌ ํŽ˜์ด์ง€๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ(404, 500, ๋“ฑ๋“ฑ) ํ‘œ์‹œ๋˜๋Š” ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

์—๋Ÿฌ ํŽ˜์ด์ง€๋Š” ๋ ˆ์ด์•„์›ƒ์ด์ง€๋งŒ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ํ‘œ์‹œ๋˜๋Š” ํŠน๋ณ„ํ•œ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ด์•ผํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ ์ปดํฌ๋„ŒํŠธ์™€ ๋‹ฌ๋ฆฌ <Nuxt /> ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•ด์„  ์•ˆ๋˜๊ณ  , layouts ๋””๋ ‰ํ† ๋ฆฌ์— ์œ„์น˜ํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค๋ฅธ ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์„์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

layouts/error.vue
<template>    <div>        <h1 v-if="error.statusCode === 404">Page not found</h1>        <h1 v-else>An error occurred</h1>        <NuxtLink to="/">Home page</NuxtLink>    </div></template>
<script>    export default {        props: ['error'],        layout: 'error', // you can set a custom layout for the error page    };</script>

Document: App.html#

App ํ…œํ”Œ๋ฆฟ์€ head์™€ body์— ๋Œ€ํ•œ ์ปจํ…์ธ ์™€ ๋ณ€์ˆ˜๋ฅผ ์ฃผ์ž…ํ•˜๋Š” Nuxt.js ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜์‹ค์ œ HTML ํ”„๋ ˆ์ž„์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

์ด ํŒŒ์ผ์€ ์ž๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๊ณ , ์ผ๋ฐ˜์ ์œผ๋กœ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ๊ฑฐ์˜ ์—†๋‹ค.

ํ•œ ๊ฐ€์ง€ ์‚ฌ์šฉ ์˜ˆ์‹œ๋Š” IE์— ๋Œ€ํ•œ ์กฐ๊ฑด๋ถ€ CSS ๋Œ€์‘์ด๋‹ค.

app.html
<!DOCTYPE html><!--[if IE 9]><html class="lt-ie9 ie9" {{ HTML_ATTRS }}><![endif]--><!--[if (gt IE 9)|!(IE)]><!--><html {{ HTML_ATTRS }}><!--<![endif]-->    <head {{ HEAD_ATTRS }}>        {{ HEAD }}    </head>    <body {{ BODY_ATTRS }}>        {{ APP }}    </body></html>

์ฐธ๊ณ ์ž๋ฃŒ#

Nuxt.js - Concept/Views

ยท ์•ฝ 1๋ถ„

Nuxt.js ๊ณต์‹ ๋ฌธ์„œ์˜ Rendering๊ณผ Deployment Target ๋ถ€๋ถ„์„ ์ฝ์œผ๋ฉด์„œ nuxt.config.js์˜ ssr: 'true'์™€ target: 'server'์˜ ์ฐจ์ด๊ฐ€ ๋ฌด์—‡์ธ์ง€ ํ—ท๊ฐˆ๋ ธ๋‹ค.

static site๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” target: 'static'๊ณผ ssr: 'true'๋ฅผ ํ™œ์„ฑํ™” ํ•ด์•ผํ•œ๋‹ค. mode: 'universal'๋Š” ๊ฐ™์€ ๊ธฐ๋Šฅ์ด์ง€๋งŒ, deprecated ๋˜์—ˆ๋‹ค.

ssr: 'true'์ด ๊ธฐ๋ณธ๊ฐ’์ด๋‹ค.

ssr: 'false'๋Š” mode: 'spa'์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด์ง€๋งŒ deprecated ๋˜์—ˆ๋‹ค. ssr: 'false'๋Š” target: 'static'๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

Nuxt.js์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ช…๋ น์–ด๊ฐ€ ์žˆ๋Š”๋ฐ, target ๊ฐ’์— ๋”ฐ๋ผ์„œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค.

์•„๋ž˜์˜ ํ‘œ๊ฐ€ ์†์„ฑ ๊ฐ’์— ๋”ฐ๋ฅธ ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ๊ธฐ๋Œ€ ๋™์ž‘์„ ์ž˜ ์„ค๋ช…ํ•ด์ค€๋‹ค.

Available commands and rendering

์ฐธ๊ณ ์ž๋ฃŒ#

Demystify Nuxtสผs target, mode, and ssr properties Going Full Static

ยท ์•ฝ 1๋ถ„

yarn run vs npm run#

yarn run [script] [\<args>]#

  • scripts ์— ๋“ฑ๋ก๋œ ๋ช…๋ น์–ด ์‹คํ–‰
  • node_modules/.bin/์— ์œ„์น˜ํ•œ ์‹คํ–‰ ๊ฐ€๋Šฅ ํŒŒ์ผ(executable) ์‹คํ–‰
  • run ์€ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.

npm run-script \<command>#

  • npm run <command> ๋กœ alias
  • scripts ์— ๋“ฑ๋ก๋œ ๋ช…๋ น์–ด ์‹คํ–‰

npm exec (npx)#

  1. node_modules/.bin ์— ์œ„์น˜ํ•œ ์‹คํ–‰ ๊ฐ€๋Šฅ ํŒŒ์ผ(executable) ์‹คํ–‰
  2. remote npm package ์‹คํ–‰

nuxt ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ#

yarn create nuxt-app# npx create-nuxt-app

create-nuxt-app ์„ ์‚ฌ์šฉํ•˜๋ฉด cli๋ฅผ ํ†ตํ•ด ํ”„๋กœ์ ํŠธ์— ๊ด€ํ•œ ์„ค์ •์— ๋Œ€ํ•œ ์งˆ์˜๋ฅผ ํ•œ๋‹ค .

image-20210707155912520