nuxtjs筆記

安裝

yarn create nuxt-app <項(xiàng)目名>

端口

// package.json

"config": {
    "nuxt": {
      "host": "127.0.0.1",
      "port": "3001"
    }
}

跨域 nuxt.config.js

modules: [
    '@nuxtjs/axios',
    '@nuxtjs/proxy'
],
axios: {
    proxy: true
},
proxy: {
    '/api': {
        // todo
    }
}

預(yù)處理器sass

安裝  node-sass sass-loader

就可以使用

<style lang="sass">
.red
  color: red
</style>

默認(rèn)模板

export default {
  //這里聲明我用search模板,那就不是用default模板了
  layout: 'search'
}

路徑引用 絕對路徑

~assets
~pages

隱藏路由

文件名前加_

|-- pages
    |-- _about.vue
    |-- index.vue
    
    ===>
    
routes: [
  {
    path: '/',
    component: '~pages/index.vue'
  }
]

Vue.use 的插件

// 需要在 plugins 中新建一個 js 的文件
import Vue from 'vue'
import VueNotifications from 'vue-notifications'
Vue.use(VueNotifications)


// 然后需要在 nuxt.config.js 的 plugins 中增加其配置 :
module.exports = {
  plugins: ['~/plugins/vue-notifications']
}

部署 ? pm2 ?

"scripts": {
    "build": "nuxt build && nuxt start",
}

pm2 start ./node_modules/nuxt/bin/nuxt -- start

less全局變量的配置

使用less先安裝less less-loader

nuxt.config.js中配置

css: [
    'element-ui/lib/theme-chalk/index.css',
    '@assets/styles/global.less'
],
modules: [
    '@nuxtjs/axios',
    '@nuxtjs/style-resources'
],
styleResources:{
    less:[
      './assets/styles/variable.less'
    ]
}

styleResources 配置的資源路徑不能使用 ~ 和 @,要使用絕對或者相對路徑

普通less

安裝 less 和 less-loader 

安裝富文本編輯器

ckeditor4-vue

plugins下新建 ckeditor4-vue.js

import Vue from "vue";
import CKEditor from "ckeditor4-vue";

Vue.use(CKEditor);

nuxt.config.js下引用

plugins: [
    "@/plugins/element-ui",
    { src: "@/plugins/ckeditor4-vue", ssr: false } // 富文本最好使用nossr ,基本都不支持window
]

最后組件中直接使用

<ckeditor v-model="defaultValue"></ckeditor>

vue-quill-editor

https://github.com/surmon-china/surmon-china.github.io/tree/source/projects/vue-quill-editor/nuxt

高亮

<template>
  <div class="example">
    <quill-editor
      class="editor"
      ref="myTextEditor"
      :value="content"
      :options="editorOption"
      @change="onEditorChange"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @ready="onEditorReady($event)"
    />
    <div class="output code">
      <code class="hljs" v-html="contentCode"></code>
    </div>
    <div class="output ql-snow">
      <div class="ql-editor" v-html="content"></div>
    </div>
  </div>
</template>

<script>
  import dedent from 'dedent'
  import hljs from 'highlight.js'
  import debounce from 'lodash/debounce'
  import { quillEditor } from 'vue-quill-editor'
  // highlight.js style
  import 'highlight.js/styles/tomorrow.css'
  // import theme style
  import 'quill/dist/quill.core.css'
  import 'quill/dist/quill.snow.css'
  export default {
    name: 'quill-example-snow',
    title: 'Theme: snow',
    components: {
      quillEditor
    },
    data() {
      return {
        editorOption: {
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike'],
              ['blockquote', 'code-block'],
              [{ 'header': 1 }, { 'header': 2 }],
              [{ 'list': 'ordered' }, { 'list': 'bullet' }],
              [{ 'script': 'sub' }, { 'script': 'super' }],
              [{ 'indent': '-1' }, { 'indent': '+1' }],
              [{ 'direction': 'rtl' }],
              [{ 'size': ['small', false, 'large', 'huge'] }],
              [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
              [{ 'font': [] }],
              [{ 'color': [] }, { 'background': [] }],
              [{ 'align': [] }],
              ['clean'],
              ['link', 'image', 'video']
            ],
            syntax: {
              highlight: text => hljs.highlightAuto(text).value
            }
          }
        },
        content: dedent`
          <h1 class="ql-align-center"><span class="ql-font-serif" style="background-color: rgb(240, 102, 102); color: rgb(255, 255, 255);"> I am snow example! </span></h1><p><br></p><p><span class="ql-font-serif">W Can a man still be brave if he's afraid? That is the only time a man can be brave. </span></p><p><br></p><p><strong class="ql-font-serif ql-size-large">Courage and folly is </strong><strong class="ql-font-serif ql-size-large" style="color: rgb(230, 0, 0);">always</strong><strong class="ql-font-serif ql-size-large"> just a fine line.</strong></p><p><br></p><p><u class="ql-font-serif">There is only one God, and his name is Death. And there is only one thing we say to Death: "Not today."</u></p><p><br></p><p><em class="ql-font-serif">Fear cuts deeper than swords.</em></p><p><br></p><pre class="ql-syntax" spellcheck="false"><span class="hljs-keyword">const</span> a = <span class="hljs-number">10</span>;
          <span class="hljs-keyword">const</span> editorOption = { <span class="hljs-attr">highlight</span>: <span class="hljs-function"><span class="hljs-params">text</span> =&gt;</span> hljs.highlightAuto(text).value };</pre><p><br></p><p><span class="ql-font-serif">Every flight begins with a fall.</span></p><p><br></p><p><a  rel="noopener noreferrer" target="_blank" class="ql-font-serif ql-size-small" style="color: rgb(230, 0, 0);"><u>A ruler who hides behind paid executioners soon forgets what death is. </u></a></p><p><br></p><iframe class="ql-video ql-align-center" frameborder="0" allowfullscreen="true" src="https://www.youtube.com/embed/QHH3iSeDBLo?showinfo=0" height="238" width="560"></iframe><p><br></p><p><span class="ql-font-serif">Hear my words, and bear witness to my vow. Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the fire that burns against the cold, the light that brings the dawn, the horn that wakes the sleepers, the shield that guards the realms of men. I pledge my life and honor to the Night’s Watch, for this night and all the nights to come.</span></p><p><br></p><p><span class="ql-font-serif">We are born to suffer, to suffer can make us strong.</span></p><p><br></p><p><span class="ql-font-serif">The things we love destroy us every time.</span></p>
        `,
      }
    },
    methods: {
      onEditorChange: debounce(function(value) {
        this.content = value.html
      }, 466),
      onEditorBlur(editor) {
        console.log('editor blur!', editor)
      },
      onEditorFocus(editor) {
        console.log('editor focus!', editor)
      },
      onEditorReady(editor) {
        console.log('editor ready!', editor)
      }
    },
    computed: {
      editor() {
        return this.$refs.myTextEditor.quill
      },
      contentCode() {
        return hljs.highlightAuto(this.content).value
      }
    },
    mounted() {
      console.log('this is Quill instance:', this.editor)
    }
  }
</script>

<style lang="scss" scoped>
  .example {
    display: flex;
    flex-direction: column;
    .editor {
      height: 40rem;
      overflow: hidden;
    }
    .output {
      width: 100%;
      height: 20rem;
      margin: 0;
      border: 1px solid #ccc;
      overflow-y: auto;
      resize: vertical;
      &.code {
        padding: 1rem;
        height: 16rem;
      }
      &.ql-snow {
        border-top: none;
        height: 24rem;
      }
    }
  }
</style>

圖片上傳

http://www.reibang.com/p/9e4e4d955d0f

<Upload
    :show-upload-list="false"
    :on-success="handleSuccess"
    :format="['jpg','jpeg','png','gif']"
    :max-size="2048"
    multiple
    action="/file/upload"
    >
    <Button icon="ios-cloud-upload-outline" ></Button>
</Upload>

handleSuccess (res) {
    // 獲取富文本組件實(shí)例
    let quill = this.$refs.QuillEditor.quill
    // 如果上傳成功
    if (res) {
        // 獲取光標(biāo)所在位置
        let length = quill.getSelection().index;
        // 插入圖片缠俺,res為服務(wù)器返回的圖片鏈接地址
        quill.insertEmbed(length, 'image', res)
        // 調(diào)整光標(biāo)到最后
        quill.setSelection(length + 1)
    } else {
        // 提示信息显晶,需引入Message
        Message.error('圖片插入失敗')
    }
}

自定義錯誤/404頁面

layouts/error.vue

return error({ message: "文章不存在", statusCode: 404 });
<template>
  <div class="container">
    <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: "default" // you can set a custom layout for the error page   您可以為錯誤頁設(shè)置自定義布局
};
</script>

全局配置 <nuxt-link> 組件默認(rèn)的激活類名。

https://zh.nuxtjs.org/api/configuration-router/#linkactiveclass

nuxt.config.js

module.exports = {
  router: {
    linkActiveClass: 'active-link'
  }
}

為應(yīng)用的每個頁面設(shè)置默認(rèn)的中間件壹士。

nuxt.config.js

module.exports = {
  router: {
    // 在每頁渲染前運(yùn)行 middleware/user-agent.js 中間件的邏輯
    middleware: 'user-agent'
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末磷雇,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子躏救,更是在濱河造成了極大的恐慌唯笙,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盒使,死亡現(xiàn)場離奇詭異崩掘,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)少办,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門苞慢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人凡泣,你說我怎么就攤上這事枉疼。” “怎么了鞋拟?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵骂维,是天一觀的道長。 經(jīng)常有香客問我贺纲,道長航闺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮潦刃,結(jié)果婚禮上侮措,老公的妹妹穿的比我還像新娘。我一直安慰自己乖杠,他們只是感情好分扎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胧洒,像睡著了一般畏吓。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上卫漫,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天菲饼,我揣著相機(jī)與錄音,去河邊找鬼列赎。 笑死宏悦,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的包吝。 我是一名探鬼主播饼煞,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼漏策!你這毒婦竟也來了派哲?” 一聲冷哼從身側(cè)響起臼氨,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤掺喻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后储矩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體感耙,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年持隧,在試婚紗的時候發(fā)現(xiàn)自己被綠了即硼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡屡拨,死狀恐怖只酥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情呀狼,我是刑警寧澤裂允,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站哥艇,受9級特大地震影響绝编,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一十饥、第九天 我趴在偏房一處隱蔽的房頂上張望窟勃。 院中可真熱鬧,春花似錦逗堵、人聲如沸秉氧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谬运。三九已至,卻和暖如春垦藏,著一層夾襖步出監(jiān)牢的瞬間梆暖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工掂骏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留轰驳,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓弟灼,卻偏偏與公主長得像级解,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子田绑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354