ã¤ã³ã¹ãã¼ã«
ããã§ã¯ã4 ã¤ã®ã¹ãããã§ãNuxt ããã¸ã§ã¯ããç«ã¡ä¸ãã§å®è¡ããæ¹æ³ã«ã¤ãã¦èª¬æãã¾ãã
ãªã³ã©ã¤ã³ãã¬ã¤ã°ã©ã³ã
CodeSandbox ã StackBlitz ã§ç´æ¥ Nuxt ããªã³ã©ã¤ã³ã§è©¦ããã¨ãã§ãã¾ãï¼
CodeSandbox ã§è©¦ã StackBlitz ã§è©¦ãåææ¡ä»¶
- Node.js - ææ°ã® LTS ãã¼ã¸ã§ã³ã®ã¤ã³ã¹ãã¼ã«ãæ¨å¥¨ãã¾ãã
- ããã¹ãã¨ãã£ã¿ãVS Code 㨠Vetur æ¡å¼µãã¾ã㯠WebStorm ãæ¨å¥¨ãã¾ãã
- ã¿ã¼ããã«ã VS Code ã« çµ±åãããã¿ã¼ããã« ã¾ã㯠WebStorm ã¿ã¼ããã« ãæ¨å¥¨ãã¾ãã
create-nuxt-app ã®ä½¿ç¨
ç´ æ©ãå§ããããã«ãcreate-nuxt-app ã使ç¨ã§ãã¾ãã
yarn ã npx ã®ã¤ã³ã¹ãã¼ã« (npx 㯠npm 5.2 ããããã©ã«ãæä¾ããã¦ãã¾ã)ãã¾ã㯠npm v6.1 以ä¸ãã¤ã³ã¹ãã¼ã«ããã¦ãããã¨ã確èªãã¦ãã ããã
yarn create nuxt-app <project-name>
npx create-nuxt-app <project-name>
npm init nuxt-app <project-name>
å§ããã¨ãããã¤ãã®è³ªåã表示ããã¾ã (ååãNuxt ãªãã·ã§ã³ãUI ãã¬ã¼ã ã¯ã¼ã¯ãTypeScriptããªã³ã¿ã¼ããã¹ããã¬ã¼ã ã¯ã¼ã¯ããã®ä»)ããã¹ã¦ã®ãªãã·ã§ã³ã«ã¤ãã¦ã¯ãcreate-nuxt-app documentation ã確èªãã¦ãã ããã
ä¸åº¦å ¨ã¦ã®è³ªåã«çããã¨ãå ¨ã¦ã®ä¾åé¢ä¿ãã¤ã³ã¹ãã¼ã«ããã¾ãã次ã®ã¹ãããã¯ãããã¸ã§ã¯ããã©ã«ãã«ç§»åãã¦ãããã¸ã§ã¯ããèµ·åãã¾ã:
cd <project-name>
yarn dev
cd <project-name>
npm run dev
ã¢ããªã±ã¼ã·ã§ã³ã¯ãä» http://localhost:3000 ã§åä½ãã¦ãã¾ãããã¤ãããã¾ã§ããï¼
æåã¤ã³ã¹ãã¼ã«
1 ã¤ã®ãã¡ã¤ã«ã¨ 1 ã¤ã®ãã£ã¬ã¯ããªãç¨æããã ã㧠Nuxt ããã¸ã§ã¯ããã¹ã¯ã©ããã§ä½ãã¾ãã
ä»åã®ä¾ã§ã¯ãã¿ã¼ããã«ã使ã£ã¦ãã£ã¬ã¯ããªã¨ãã¡ã¤ã«ãä½æãã¾ãããã好ã¿ã®ã¨ãã£ã¿ã§ä½æãã¦æ§ãã¾ããã
ããã¸ã§ã¯ãã®ã»ããã¢ãã
å§ããã«ã¯ããã¸ã§ã¯ãã®ååãã¤ãã空ã®ãã£ã¬ã¯ããªãä½æããä½æãããã£ã¬ã¯ããªã«ç§»åãã¾ã:
mkdir <project-name>
cd <project-name>
<project-name>
ã¯ãèªèº«ã®ããã¸ã§ã¯ãåã«ç½®ãæãã¦ãã ããã
package.json
ãä½æãã¾ã:
touch package.json
package.json
ãã¨ãã£ã¿ã§éãã以ä¸ã® JSON ã®å
容ãå
¥åãã¾ã:
{
"name": "my-app",
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"generate": "nuxt generate",
"start": "nuxt start"
}
}
scripts
ã« npm run <command>
ã¾ã㯠yarn <command>
ã§èµ·åããã Nuxt ã³ãã³ããå®ç¾©ãã¾ãã
package.json ãã¡ã¤ã«ã¨ã¯ä½ã§ããï¼
package.json
ã¯ããã¸ã§ã¯ãã«ããã ID ã«ã¼ãã®ãããªãã®ã§ãããã package.json
ãã¡ã¤ã«ãã©ããªãã®ãç¥ããªãå ´å㯠npm ããã¥ã¡ã³ã ãä¸èªãã¦ãã ããã
Nuxt ã®ã¤ã³ã¹ãã¼ã«
package.json
ãä½æå¾ã以ä¸ã®ããã«ãnpm
ã¾ã㯠yarn
çµç±ã§ããã¸ã§ã¯ãã« nuxt
ã追å ãã¾ã:
yarn add nuxt
npm install nuxt
ãã®ã³ãã³ã㯠nuxt
ãä¾åé¢ä¿ã¨ãã¦ããã¸ã§ã¯ãã«è¿½å ãã¾ããpackage.json
ã«ãèªåçã«è¿½å ãã¾ããã¾ããnode_modules
ãã£ã¬ã¯ããªãä½æãããã¤ã³ã¹ãã¼ã«ããããã±ã¼ã¸ã¨ä¾åé¢ä¿ãä¿åããã¾ãã
yarn.lock
ã¾ã㯠package-lock.json
ãä½æãããããã¸ã§ã¯ãã«ã¤ã³ã¹ãã¼ã«ãããããã±ã¼ã¸ã®ä¸è²«ããã¤ã³ã¹ãã¼ã«ã¨äºææ§ã®ããä¾åé¢ä¿ãä¿è¨¼ãã¾ããæåã®ãã¼ã¸ãä½æ
Nuxt 㯠pages
ãã£ã¬ã¯ããªå
ã«ããå
¨ã¦ã® *.vue
ãã¡ã¤ã«ãã¢ããªã±ã¼ã·ã§ã³ã®ã«ã¼ãã¨ãã¦å¤æãã¾ãã
ããã¸ã§ã¯ãã« pages
ãã£ã¬ã¯ããªãä½æãã¾ã:
mkdir pages
ããã¦ãpages
ãã£ã¬ã¯ããªå
ã« index.vue
ãã¡ã¤ã«ãä½æãã¾ã:
touch pages/index.vue
ãã®ãã¼ã¸ã index.vue
ã¨åä»ãããã¨ãéè¦ã§ããããã¯ã¢ããªã±ã¼ã·ã§ã³ãéããéã« Nuxt ãããã©ã«ãã§è¡¨ç¤ºãããã¼ã¸ã«ãªãã¾ããããããã¼ã ãã¼ã¸ã§ãããindex ã¨åã¥ããªãã°ãªãã¾ããã
index.vue
ãã¡ã¤ã«ãã¨ãã£ã¿ã§éãã次ã®å
容ã追å ãã¾ã:
<template>
<h1>Hello world!</h1>
</template>
ããã¸ã§ã¯ãã®éå§
ã¿ã¼ããã«ã§ä»¥ä¸ã®ã³ãã³ãã®ã©ã¡ãããå ¥åãã¦ããã¸ã§ã¯ããå®è¡ãã¾ã:
yarn dev
npm run dev
ä»ãã¢ããªã±ã¼ã·ã§ã³ã¯ http://localhost:3000 ã§åä½ãã¦ãã¾ãã
ã¿ã¼ããã«ã®ãªã³ã¯ãã¯ãªãã¯ãã¦ãã©ã¦ã¶ãéãã¨ãåã®ã¹ãããã§ã³ãã¼ãããHello Worldãã®ããã¹ãã表示ããã¾ãã
ãã¼ãã¹ã¹ããã
pages
ãã£ã¬ã¯ããªã« fun.vue
ã¨ããååã®ãã¼ã¸ãä½æãã¾ãããã
<template></template>
ã追å ããã¿ã°å
ã«ãããããæç« ãå
¥ããè¦åºããå
¥ãã¾ãã
ããã¦ããã©ã¦ã¶ã§æ°ãããã¼ã¸ localhost:3000/fun ã«ã¢ã¯ã»ã¹ãã¾ãã
more-fun
ã¨ããååã®ãã£ã¬ã¯ããªãä½æãããã®ä¸ã« index.vue
ãã¡ã¤ã«ãå
¥ãã¾ããããã¯ãmore-fun.vue
ãã¡ã¤ã«ãä½æããã®ã¨åãçµæãå¾ããã¾ãã