Qiitaåæçš¿ã§ãã
Nuxt.jsã«è§Šããæ©äŒããã£ãã®ã§ãåå¿é²ãšããŠæçš¿ãããŠããããŸãã
##Nuxt.jsãšã¯
Nuxt.jsãšã¯ãVue.jsã®ãã¬ãŒã ã¯ãŒã¯ã®ïŒã€ã§ãã
äž»ãªç¹åŸŽãšããŠã
- Vue-routerã:ãã¢ããªã±ãŒã·ã§ã³å ã®ããŒãžé·ç§»ïŒã«ãŒãã£ã³ã°ïŒã管çããããã±ãŒãž
- vuexã:ãã¢ããªã±ãŒã·ã§ã³å ã®ããŒã¿ãç¶æ ã®ç®¡çãè¡ãããã±ãŒãž
ãªã©ãæããããŸãã
ãã®ä»ã«ããããã©ã«ãã§SSRãè¡ããããšåå¿è
ã«ãããããããéåžžã«äŸ¿å©ã§ãã
SSRãšã¯ãèŠã¯
ãåŸæ¥ã®ã¯ã©ã€ã¢ã³ãã»ãµãŒããŒéã®åŠçã®å€§éšåããµãŒããŒãæ
ã£ãŠããããããåŠçãéåžžã«æ©ããªãä»çµã¿ã
çšåºŠã§èããŠããã£ãŠãããšæããŸãã
ã¡ãªã¿ã«ãSSRãªã©ã®æŠå¿µã¯ãã®èšäºãéåžžã«ããããããã£ãã§ãã
Nuxtã¢ããªã±ãŒã·ã§ã³ã®çæ
ã§ã¯æ©éãNuxtãã©ã«ãã®å®è£ ãè¡ã£ãŠãããŸãã
yarnã®ã€ã³ã¹ããŒã«ãåºæ¥ãŠããªããã°ãŸãæåã«ãã®ã³ãã³ããè¡ãªã£ãŠãã ããã
sudo npm install -g yarn
yarnãæ¢ã«å ¥ã£ãŠãã人ã¯ãNuxtã¢ããªã±ãŒã·ã§ã³ãçæããŸãããã
yarn create nuxt-app çæãã©ã«ãå
ã
ã
ã³ãã³ããå®è¡ãããšããã¡ããã¡ã質åãããŸãã
ã¢ããªã±ãŒã·ã§ã³ã®åå
? Project name (QiitaExample)
åºæ¬çã«ã¯ãã®ãŸãŸEnterã§ããã§ãã
ã¢ããªã±ãŒã·ã§ã³ã®èª¬æ
? Project description (My exquisite Nuxt.js project)
åºæ¬çã«ã¯ãã®ãŸãŸEnterã§ããã§ãã
äœè ã®åå
? Author name (Your name)
ãããåºæ¬çã«ã¯ãã®ãŸãŸEnterã§ããã§ãã
####ããã±ãŒãžãããŒãžã£ãŒã®éžæ
? Choose the package manager (Use arrow keys)
⯠Yarn
Npm
ããã±ãŒãžãããŒãžã£ãŒã«ã«npm
ãyarn
ã®ã©ã¡ãã䜿ãããéžã¹ãŸãã
UIãã¬ãŒã ã¯ãŒã¯ã®éžæ
? Choose UI framework (Use arrow keys)
⯠None
Ant Design Vue
Bootstrap Vue
Buefy
Bulma
Element
Framevuerk
iView
Tachyons
Tailwind CSS
Vuetify.js
UIãã¬ãŒã ã¯ãŒã¯ã«äœã䜿ããã§ãã(ä»åã¯ãVuetify.js
ã䜿ã£ãŠããããšæããŸãã)
ãµãŒããŒãµã€ãã®ãã¬ãŒã ã¯ãŒã¯ã®éžæ
? Choose custom server framework (Use arrow keys)
⯠None (Recommended)
AdonisJs
Express
Fastify
Feathers
hapi
Koa
Micro
ãµãŒããŒãµã€ãã®ãã¬ãŒã ã¯ãŒã¯ã§ãã
TypeScript
ãªã©ã䜿ãéã¯Express
ãè¯ããšæããŸãããä»åã¯Noneã§ä»¥äžè¡ããŸãã
Nuxtã¢ãžã¥ãŒã«ã®éžæ
? Choose Nuxt.js modules (Press <space> to select, <a> to toggle all, <i> to inv
ert selection)
â¯â¯ Axios
⯠Progressive Web App (PWA) Support
⯠DotEnv
ä»åã¯firebase
ã§ãµãŒããŒãšçµã³ã€ããããDotEnv
ãéžæããŸãã
ïŒåŸã
玹ä»ããŸãããDotEnvã¯ãµãŒããŒæ
å ±ãé ããŠãããããŒã«ã§ããïŒ
ã³ãŒããã©ãŒããã¿ãŒã®éžæ
? Choose linting tools (Press <space> to select, <a> to toggle all, <i> to inver
t selection)
â¯â¯ ESLint
⯠Prettier
⯠Lint staged files
⯠StyleLint
ããŸããããããªãã³ãŒãããã«ãŒã«ã«åã£ãŠã³ãŒãæŽåœ¢ããŠãããããŒã«ã§ãã
ä»åã¯äœãéžæããŸããã
####ãã¹ããã¬ãŒã ã¯ãŒã¯ã®éžæ
? Choose test framework (Use arrow keys)
⯠None
Jest
AVA
ããã¯æ£çŽéããããããŸãããïŒç¬ïŒ
ãªã®ã§ãä»åã¯Noneã«ããŸãã
ïŒè©³ããæ¹ã¯æããŠãã ãããšå©ãããŸããïŒ
####ã¬ã³ããªã³ã°æ¹æ³
? Choose rendering mode (Use arrow keys)
⯠Universal (SSR)
Single Page App
åé ã§è¿°ã¹ãSSRãSPAã®éžæãåºæ¥ãŸãã
ä»åã¯SSRãéžæããŸãã
ïŒSSRã»SPAã®æŠå¿µãããããªã人ã¯ãã®èšäºãããããã§ããïŒ
####ãããããã¡ã³ãããŒã«ã®éžæ
? Choose development tools (Press <space> to select, <a> to toggle all, <i> to i
nvert selection)
â¯â¯ jsconfig.json (Recommended for VS Code)
Visual Studioã䜿ãéã¯å
¥ããŠãããã»ããããããã§ãã
åã¯Atomã䜿ãã®ã§ãéžæãããŠããŸããã
ð Successfully created project QiitaExample
To get started:
cd QiitaExample
npm run dev
To build & start for production:
cd QiitaExample
npm run build
npm run start
âš Done in 2160.69s.
ãã®ç»é¢ã衚瀺ããããç¡äºã«åæèšå®ãçµäºã§ãïŒ
Nuxtã®å®è¡
ã§ã¯æ©éèµ·åããŠã¿ãŸãããã
ã
èªåãäœã£ãããã©ã«ãã«ç§»åããŸãã
cd çæãã©ã«ãå
ãã®åŸnpm run dev
ã§å®è¡åºæ¥ãŸãã
npm run dev
~:QiitaExample yourname$ npm run dev
> QiitaExample@1.0.0 dev /~/QiitaExample
> nuxt
âââââââââââââââââââââââââââââââââââââââââââââââ®
â â
â Nuxt.js v2.11.0 â
â Running in development mode (universal) â
â â
â Listening on: http://localhost:3000/ â
â â
â°ââââââââââââââââââââââââââââââââââââââââââââââ¯
â¹ Preparing project for development 17:38:51
â¹ Initial build may take a while 17:38:51
â Builder initialized 17:38:51
â Nuxt files generated 17:38:51
â Client
Compiled successfully in 6.37s
â Server
Compiled successfully in 5.58s
â¹ Waiting for file changes 17:38:59
â¹ Memory usage: 322 MB (RSS: 463 MB) 17:38:59
ãã®ç»é¢ã衚瀺ãããã°ãç¡äºã«å®è¡åºæ¥ãŠããŸãã
å®è¡äžã¯ãhttp://localhost:3000/ ã«ç·šéäžã®ç»é¢ã衚瀺ãããŸãã
ã§ã¯ãhttp://localhost:3000/ ã確èªããŠã¿ãŸãããã
ãããªæãã«ãªãã¯ãã§ãã
Nuxtã®ãã£ã¬ã¯ããªæ§é
ã§ã¯æ©éçæãããå®è¡ãã©ã«ãã®äžèº«ãã¿ãŠãããŸãããã
assets
äž»ã«ç»åããŒã¿ãªã©ãèšè¿°ãããã©ã«ãã«ãªããŸãã
ãã¡ãã¯ãwebpack
ã¢ãžã¥ãŒã«ãšããŠæ±ãããšãã§ããŸãã
webpack
ã¢ãžã¥ãŒã«ã¯ããç»åããŒã¿ãURLãšããŠæ±ããããã«ãªãããéåžžã«åŠçãæ©ããªããã®ããšããèªèã§å€§äžå€«ã ãšæããŸãã
ãã ããããŒã¿ãµã€ãºã®å€§ããªãã®ãæ±ã£ãŠããŸããšããã£ãŠåŠçãé
ããªãã®ã§ã现ã
ãããã®ããã¹ãã§ãã
components
é »ç¹ã«äœ¿ãããŽããããããŒãããã¿ãŒãªã©ãèšè¿°ããŸãã
ããã«é圢ãäœã£ãŠãããšã¢ããªã±ãŒã·ã§ã³å
ã®pages
ãlayouts
ã§ç°¡åã«åŒã³åºãããšãã§ããŸãã
layouts
ã¢ããªã±ãŒã·ã§ã³ã®å€§ãŸããªå€èŠ³ã決ããããšãã§ããŸãã
ãŸãåºæ¬çã«/layouts/default.vue
ãããpages
ã衚瀺ãããŠãããšããåãæ¹ã«ãªããŸãã
åŒã³åºãæ¹ã¯ãäžã®ããã«<nuxt />
ã§åŒã³åºããŸãã
ãŸãããäžæãªããŒãžã«é·ç§»ããããªã©ã®ãšã©ãŒæã«ã¯layouts/error.vue
ãåŒã³åºãããŸãã
<template>
<nuxt/>
</template>
middlware
ãã®èšäºã«è¯ãç»åããã£ãã®ã§äœ¿ãããŠããããŸãã
ãã®ããã«ãpages
ãlayouts
ãã¬ã³ããªã³ã°ïŒããŒãžãçæïŒããã床ã«ããã®åã«åŒã³åºãããåŠçãèšè¿°ãããã£ã¬ã¯ããªã«ãªããŸãã
ããã«ããªãã€ã¬ã¯ãåŠçãªã©ãèšè¿°ãããšç¶ºéºã«æ©èœããŸãã
pages
ã¡ã€ã³ã§èšè¿°ããŠããéšåã«ãªããŸãã
layouts
ã«è¡šç€ºããŠãããããŒãžãããã«èšè¿°ããŸãã
ãŸãåé ã§è¿°ã¹ãéããVue-router
ãæšæºè£
åãããŠãããããç¹ã«ã«ãŒãã£ã³ã°ãªã©ã¯æ°ã«ããªããŠã倧äžå€«ã§ãã
###plugins
å€éšã¢ãžã¥ãŒã«ãªã©ã詳ããèšè¿°ããããšãã§ããŸãã
ä»åã¯firebase
ã®æ
å ±ããããã«èšè¿°ããŸãã
ãã ãã䜿ãéã¯ãnuxt.config.js
ã®modules
ã«ãèšè¿°ããå¿
èŠããããŸãã
###static
assetsãšåãããã«ãç»åããŒã¿ãèšè¿°ãããã©ã«ãã§ãã
ãã¡ãã¯webpack
ã¢ãžã¥ãŒã«ãšããŠã¯æ±ããªãã®ã§ãasset
ã§æ±ããªããããªå€§ããªããŒã¿ãµã€ãºã®ãã®ãªã©ãã¡ã€ã³ã«èšè¿°ããŠãããŸãã
###store
ã¢ããªã±ãŒã·ã§ã³å
ã®ããŒã¿ãæ
å ±ã®ä¿æã»æäœãè¡ããã©ã«ãã§ãã
æšæºè£
åãããŠããVuexã®é åã§ããããŸãã
ä»åã¯ãã°ã€ã³ç¶æ
ããã¹ã¯ãŒããªã©ãèšè¿°ããŠãããŸãã
###nuxt.config.js
ãã®ãã¡ã€ã«ã«ãã®ã¢ããªã±ãŒã·ã§ã³ã®å
容ããã£ããèšè¿°ããŠãããŸãã
ãŸããã®ãã¡ã€ã«ãããããšãnpm run dev
ãæ¢ãŸããããã¯äžå
·åãã§ãã®ã§ããããããã£ãããšã¯ããäžåºŠãã«ããããæ¹ãããã§ãã
##pagesãlayoutsã®èšè¿°æ¹æ³
åºæ¬çãªUIã®èšè¿°æ¹æ³ã¯ãHTMLãšCSSãšJSãã²ãšãŸãšãŸãã«ãªã£ããããªãã®ã§ãã
ã€ãŸã以äžã®ããã«ãªããŸãã
<!-- èŠã¯HTML -->
<template>
<v-app>
<v-content>
<!-- ããã«ãã£ã±ãã³ã³ãã³ããæžããŠãã -->
</v-content>
</v-app>
</template>
<!-- èŠã¯JS -->
<script>
//methodsãããªããã
</script>
<!-- èŠã¯CSS -->
<style>
/* templateã«ããã³ã³ãã³ãã®èŠãç®ãå€ãã */
</style>
ã§ã¯ïŒã€ãã€èŠãŠãããŸãããã
###template
äžã«ããããã«HTML
ã®ãããªåããããŸãã
ä»åã¯UIãã¬ãŒã ã¯ãŒã¯ã«Vuetify
ãæ¡çšããŠããããã<v-app>
ã§å
šãŠã®Vuetifyã³ã³ããŒãã³ã
ãå²ãå¿
èŠããããŸããããã§å²ãã§ããªããšããããã®ã³ã³ããŒãã³ããäºæãã¬æåãããŸãã
ãã ããããã¯layouts/default.vue
ã§<nuxt />
ãå²ãã§ããã°ãããããpages
ã«èšè¿°ããå¿
èŠã¯ãªãã§ãã
ãã®ã»ãã®åºæ¬çãªã³ã³ããŒãã³ãã®ããšã¯å ¬åŒãã芧ãã ããã
###script
ããã§ã¯JavaScript
ã®åãã®ããã«ããã®ããŒãžã®ã¿ã®é¢æ°ãªã©ãèšè¿°ãæåã決å®ããŸãã
ãŸããã©ã°ã€ã³ãimport
ãããã®ãããã§ãã
ãŸãã¯äŸã®å
šäœåããããŸãã
<script>
import firebase from "~/plugins/firebase.js";
export default{
data() {
return {
mailAddress: '',
password: '',ã
}
},
methods: {
pushBtn: function() {
console.log("ãã¿ã³æŒãããã")
},
login: function() {
firebase.auth().signInWithEmailAndPassword(this.mailAddress, this.password)
.then(user => {
this.$store.commit('loginState', this.mailAddress, this.password);
this.$router.push("top");
}).catch((error) => {
this.valid = true;
});
},
},
created() {
console.log("createãããã")
},
};
</script>
ãã®ããã«data
ã»methods
ã»created
ãä»åã¯å€çšããŸãã
ã§ã¯ïŒã€ãã€èŠãŠã¿ãŸãããã
####data
ãã®ããŒãžã®ã¿ã§äœ¿ãå€æ°ãå®çŸ©ããŸãã
<script>
export default{
data() {
return {
mailAddress: '',
password: '',
}
},
....
}
</script>
ããã¯å€å倧äžå€«ã§ãããã
####methods
ãã®ããŒãžã§äœ¿ãé¢æ°ãå®çŸ©ããŸãã
<script>
export default{
....
methods: {
pushBtn: function() {
console.log("ãã¿ã³æŒãããã")
},
login: function() {
firebase.auth().signInWithEmailAndPassword(this.mailAddress, this.password)
.then(user => {
this.$store.commit('loginState', this.mailAddress, this.password);
this.$router.push("top");
}).catch((error) => {
this.valid = true;
});
},
},
...
}
</script>
ãã®äŸã§ã¯pushBtn
ãšããé¢æ°ãšlogin
ãšããé¢æ°ãå®çŸ©ãããŠããŸãã
次å以éã§è§ŠããŠããã®ã§ãŸã æ°ã«ããªããŠãããã®ã§ãããlogin
ã§ã¯ãæ£åžžã«äœåãããšstore
ã«å®çŸ©ãããloginState
ãšããmutations
ãåŒã³åºããäžã€pages/top.vue
ãšããå Žæã«ç»é¢é·ç§»ãããããªé¢æ°ã«ãªã£ãŠããããšãããããŸãã
ãŸãããã®this.$router.push("è¡ãå
ã®ãã¹")
ãšããæžãæ¹ããåºæ¬çãªvue-router
ã®æžãæ¹ã«ãªãã®ã§ãèŠããŠãããŸãããã
####created
ããã¯ãããŒãžãã§ãã(create)ãããã®ã¡ã«äžåäžåäœåãããã®ããšããèãæ¹ã§æŠã倧äžå€«ã§ãã
詳ããã¯å
¬åŒã®ã©ã€ããµã€ã¯ã«ãã¿ãŠã¿ãŠãã ããã
<script>
export default{
....
created() {
console.log("createãããã")
},
}
</script>
ã€ãŸãä»åã§ã¯ããã®ããŒãžã衚瀺ããããã³ã«ãcreateããããããšconsole
ãããŸãã
以äžïŒã€çŽ¹ä»ããŸãããmounted
ãªã©ä»ã«ãéèŠãªãã®ããããŸãã
æ°ã«ãªãæ¹ã¯è²ã
調ã¹ãŠã¿ãŠãã ããã
äžå¿å
¬åŒã貌ã£ãŠãããŸãããããŸãããããããã¯ãªãã§ãã
##ãŸãšã
ä»åã¯ãã£ãã°ããã«Nuxt.jsã«ã€ããŠèª¬æãããŠããããŸããã
次åãããå
·äœçãªèšè¿°ã®ä»æ¹ãæžããŠããããšæããŸãã
ãŸããééããŠããç¹ãããã°æ¯éæããŠãã ããïŒïŒ
次åã¯ãã¡ãããã©ãã