使用腳手架工具 create-nuxt-app 快速建立css
npx create-nuxt-app <項目名>
npx create-nuxt-app <項目名> html
執行一些選擇前端
修改 pages/index.vuevue
<h1 class="title">Hello world</h1>
在 localhost:3000 能夠看到更新效果node
|-- .nuxt // Nuxt自動生成,臨時的用於編輯的文件,build |-- assets // 用於組織未編譯的靜態資源入LESS、SASS 或 JavaScript |-- components // 用於本身編寫的Vue組件,好比滾動組件,日曆組件,分頁組件 |-- layouts // 佈局目錄,用於組織應用的佈局組件,不可更改。 |-- middleware // 用於存放中間件 |-- pages // 用於存放寫的頁面,咱們主要的工做區域 |-- plugins // 用於存放JavaScript插件的地方 |-- static // 用於存放靜態資源文件,好比圖片 |-- store // 用於組織應用的Vuex 狀態管理。 |-- .editorconfig // 開發工具格式配置 |-- .eslintrc.js // ESLint的配置文件,用於檢查代碼格式 |-- .gitignore // 配置git不上傳的文件 |-- nuxt.config.json // 用於組織Nuxt.js應用的個性化配置,已覆蓋默認配置 |-- package-lock.json // npm自動生成,用於幫助package的統一性設置的,yarn也有相同的操做 |-- package-lock.json // npm自動生成,用於幫助package的統一性設置的,yarn也有相同的操做 |-- package.json // npm包管理配置文件
別名 | 目錄 |
---|---|
~ 或 @ | srcDir |
~~ 或 @@ | rootDir |
在您的 vue 模板中, 若是你須要引入 assets 或者 static 目錄, 使用 ~/assets/your_image.png 和 ~/static/your_image.png 方式。webpack
在根目錄下的 package.json 裏對 config 項進行配置ios
"config":{ "nuxt":{ "host":"127.0.0.1", "port":"1818" } },
定義一個全局的 CSS 來初始化咱們的頁面渲染,這裏使用 normailze.css。git
新建 assets/css/normalize.cssgithub
修改 nuxt.config.jsweb
css: ["element-ui/lib/theme-chalk/index.css", "~assets/css/normailze.css"],
在 nuxt.config.js 中能夠對 webpack 配置進行覆蓋
build: { loaders:[ { test:/\.(png|jpe?g|gif|svg)$/, loader:"url-loader", query:{ limit:10000, name:'img/[name].[hash].[ext]' } } ], /* ** Run ESLint on save */ extend (config, { isDev, isClient }) { if (isDev && isClient) { config.module.rules.push({ enforce: 'pre', test: /\.(js|vue)$/, loader: 'eslint-loader', exclude: /(node_modules)/ }) } } }
編寫一個測試 demo (找個網站抄抄),來看看基本流程
npm install iview --save 或 vue add iview (須要vue-cli支持)
想要加入其它的配置,能夠在 nuxt.config.js 的 plugins 配置項中加入,同時在 plugins 文件夾下加入引入邏輯。例如:
nuxt.config.js
{src: '~plugins/iview', ssr: true}
同時新建 plugins/iview.js
import Vue from 'vue' import iView from 'iview' Vue.use(iView) import 'iview/dist/styles/iview.css'
可經過添加 layouts/default.vue 文件來擴展應用的默認佈局
<template> <div class="layout"> ...... </div> </template>
傳統的 spa 使用的
,但在 nuxtjs 中使用 組件顯示頁面的主體內容.created 和 data 中的邏輯,是在服務端加載時處理的,並非瀏覽器端,瀏覽器端的邏輯好比 window 或 location 等對象要在 mounted 中寫,不然會報錯.head 中定義一些元數據,這些元數據會被爬蟲抓取到,能夠在每個頁面中自定義.
網上隨便下張圖片,而後新建 pages/index
<template> <div class="container"> <div> <Card v-for="i in 5" :key="i" :style="{width:'100%',marginBottom:'15px'}"> <div style="text-align:center"> <img src="~/static/image/cat.jpg" height="300px" /> <h3>A high quality UI Toolkit based on Vue.js</h3> </div> </Card> </div> </div> </template>
@和~均可以用來引入圖片
nuxt 中的基礎路由是根據 pages 的目錄結構來生成的.例如 pages/index.vue 就是{name:'index',path:'/',component:'pages/index.vue'}, 同理 pages/books/index.vue 就是{name:'books',path:'/books',component:'pages/books/index.vue'}
修改 layout/default.vue
<Menu mode="horizontal" theme="dark" active-name="1"> <div class="layout-logo"></div> <div class="layout-nav"> <MenuItem name="1"> <Icon type="ios-navigate"></Icon> <nuxt-link to="/">首頁</nuxt-link> </MenuItem> <MenuItem name="2"> <Icon type="ios-keypad"></Icon>發現 </MenuItem> <MenuItem name="3"> <nuxt-link to="/books">小書</nuxt-link> </MenuItem> </div> </Menu>
Nuxt.js 推薦nuxt-link標籤來構建路由體系
使用 asyncData 的方法,使得咱們能夠在設置組件的數據以前能異步獲取或處理數據。asyncData 方法會在組件(限於頁面組件)每次加載以前被調用。它能夠在服務端或路由更新以前被調用。 在這個方法被調用的時候,第一個參數被設定爲當前頁面的上下文對象,你能夠利用 asyncData 方法來獲取數據,Nuxt.js 會將 asyncData 返回的數據融合組件 data 方法返回的數據一併返回給當前組件。因爲 asyncData 方法是在組件 初始化 前被調用的,因此在方法內是沒有辦法經過 this 來引用組件的實例對象。
npm install axios --save
在http://myjson.com/上建立假僞數據
{ "data": [ { "name": "Cesium基礎功能-鷹眼地圖。附帶源碼下載" }, { "name": "iOS開發簡記(7):網絡請求模塊" }, { "name": "iOS開發簡記(7):網絡請求模塊" }, { "name": "(難度Hard) Problem 132. Palindrome Partitioning II(迴文串拆分II)" }, { "name": "iOS 13適配問題" }, { "name": "Cesium基礎功能-氣泡彈窗。附帶源碼下載" }, { "name": "重學安卓:是讓人耳目一新的 Jetpack MVVM 精講啊!" }, { "name": "iOS開發簡記(6):storyboard的使用" }, { "name": "最熟悉的陌生人:5 分鐘快速理解 HTTP2" } ] }
複製粘貼,點擊保存,獲取地址https://api.myjson.com/bins/f1bdx
新建 pages/index.vue
<template> <div class="container"> <div> <Card v-for="(item,index) in books" :key="index" :style="{width:'100%',marginBottom:'15px'}"> <div style="text-align:center"> <img src="~/static/image/cat.jpg" height="300px" /> <h3>{{ item.name }}</h3> </div> </Card> </div> </div> </template> <script> import axios from 'axios' export default { async asyncData(){ let {data}=await axios.get('https://api.myjson.com/bins/f1bdx') return {info: data} } } </script>
Nuxt.js 爲頁面提供的特殊配置項。其中 head 配置當前頁面的 Meta 標籤, 詳情參考 頁面頭部配置 API。
head: { title: '一本書 - 首頁', meta: [ { hid: 'description', name: 'description', content: 'website description' }, { name: 'keywords', content: '一本書,碼農,技術,vue,node,nuxt,前端' } ] },
打開網頁->刷新->右鍵查看,效果就顯示出來了,這也是 nuxt 的一大優勢
全局動畫默認使用 page 來進行設置,例如如今咱們爲每一個頁面都設置一個進入和退出時的漸隱漸現的效果。咱們能夠先在根目錄的 assets/css 下創建一個 main.css 文件。
.page-enter-active, .page-leave-active { transition: opacity 2s; } .page-enter, .page-leave-active { opacity: 0; }
而後在 nuxt.config.js 里加入一個全局的 css 文件就能夠了。
css:['assets/css/main.css'],
目前官方設定nuxt-link組件製做跳轉連接纔有動畫
修改 pages/books/index.vue
<Row type="flex" gutter="10" justify="start" class="code-row-bg"> <Col v-for="i in 10" :key="i" span="6"> <Card style="width:100%"> <nuxt-link :to="'/books/'+i"> <div style="text-align:center"> <img style="width:100%" src="https://img3.doubanio.com/view/subject/m/public/s6974202.jpg" /> <h3>史蒂夫·喬布斯傳</h3> </div> </nuxt-link> </Card> </Col> </Row>
在 Nuxt.js 裏面定義帶參數的動態路由,須要建立對應的如下劃線做爲前綴的 Vue 文件 或 目錄。也就是要麼建立_id.vue,要麼建立_id/index.vue
新建 pages/books/_id.vue
<template> <div> <Card style="width:100%"> <div style="text-align:center"> <img style="width:100%" src="https://img3.doubanio.com/view/subject/m/public/s6974202.jpg" /> <h3>史蒂夫·喬布斯傳</h3> <p>當前id : {{ id }}</p> </div> </Card> </div> </template> <script> export default { validate({ params }) { return /^[0-9]+$/.test(params.id) }, data() { return { id: this.$route.params.id } }, head() { return { title: '史蒂夫·喬布斯傳', meta: [{ hid: 'description', name: 'books', content: '史蒂夫·喬布斯傳' }] } } } </script>
Nuxt.js 容許你在 nuxt.config.js 裏定義應用所需的全部默認 meta 標籤,在 head 字段裏配置就能夠了
新建 root/app.html
<!DOCTYPE html> <html lang="en"> <head> {{ HEAD }} </head> <body> {{ APP }} </body> </html>
nuxt 實際上已經將開發的一些需求都準備好了,使用 nuxt 進行 vue 服務端渲染可以極大的提升開發效率
靜態應用部署
npm run generate
參考