Vuepress使用Valine搭建带有评论系统的博客

Vuepress网站

vuepresscss

开始搭建vuepress-blog

# 安装
yarn global add vuepress # 或者:npm install -g vuepress

# 新建一个 markdown 文件
echo '# Hello VuePress!' > README.md

# 开始写做
vuepress dev .

# 构建静态文件
vuepress build .

接着,在 package.json 里加一些脚本:html

{
  "scripts": {
    "docs:dev": "vuepress dev docs",
    "docs:build": "vuepress build docs"
  }
}
yarn docs:dev # 或者:npm run docs:dev

固然这对于大多数人来讲都是很简单的问题,自vuepress公开之后,有太多筒子想要vuepress添加评论系统,但是目测大佬们并无这个想法,不过对于vue.js生态环境而言,给咱们不少本身动手的可能。下面请食用个人开发方法。vue

选择一个第三方评论系统

  1. gitment
  2. 来必立
  3. Valine

前二者都是有厚实的长城,因此我建议各位就别想了,虽然有搭建过的blog。若是有想了解的请看VuePress 集成第三方评论模块git

我选择了Valine,请你们看。
comment.pnggithub

看到上面是否是以为挺好看...接下来开发

Valine食用方法

Valine.js食用方法npm

开发代码

comments.vue

<template>
  <div class="vcomment" v-if="data.comments === true">
    <div id="vcomments"></div>
  </div>
</template>
<script>
import { isActive, hashRE, groupHeaders } from '../util'
export default {
  computed: {
    data () {
      return this.$page.frontmatter
    },
  },
  mounted: function(){
    this.createValine()
  },
  
  methods: {
    createValine() {
      const Valine = require('valine');
      window.AV = require('leancloud-storage')
      const valine =  new Valine({
        el: '#vcomments',
        appId: '',
        appKey: '',
        notify: false,
        verify: false,
        avatar: 'monsterid',
        path: window.location.pathname,
        placeholder: '欢迎留言与我分享您的想法...',
      });
      this.valineRefresh = false
    }
  },
  watch: {
    '$route' (to, from) {
      if(to.path !==  from.path){
        setTimeout(() => {
          //从新刷新valine
          this.createValine()
        }, 300)
      }
      
      /**
       * TODO:
       * 1. 使用其余方法更新评论组件 或者使用其余较为好用的评论组件
       * 2. 添加categories and tag
       * 3. 更换其余主题
       */
    }
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
#vcomments {
  max-width 740px
  padding 10px
  display block;
  margin-left auto;
  margin-right auto;
}
</style>

page.vue配置

page.vue

加入以上代码便可完成真个评论系统,而后 yarn dev便可看到效果!

个人开源项目地址

vuepress-blog 欢迎star!mmmjson

个人blog微信

关注个人微信公众帐号,分享更多~

微信公众帐号

相关文章
相关标签/搜索