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

Vuepress网站

vuepress

开始搭建vuepress-blog

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

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

# 开始写作
vuepress dev .

# 构建静态文件
vuepress build .

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

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

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

选择一个第三方评论系统

  1. gitment
  2. 来必立
  3. Valine
    前两者都是有厚实的长城,所以我建议各位就别想了,虽然有搭建过的blog。如果有想了解的请看VuePress 集成第三方评论模块

我选择了Valine,请大家看。
comment.png

看到上面是不是觉得挺好看…接下来开发

Valine食用方法

Valine.js食用方法

开发代码

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: 'piM1Wm7mzq4fsj7RfCCJ7slE-gzGzoHsz',
        appKey: 'vdSq43byXijVSfd0Y5qY0vf8',
        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!mmm

我的blog

关注我的微信公众账号,分享更多~

微信公众账号

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
如果你想在 Vue 中实现按钮跳转到新页面,可以使用 Vue Router。首先,你需要安装 Vue Router: ``` npm install vue-router ``` 接着,在你的 Vue 实例中引入 Vue Router 并注册路由: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ { path: '/new-page', name: 'NewPage', component: () => import('@/views/NewPage.vue') } ] const router = new VueRouter({ mode: 'history', routes }) export default router ``` 这里定义了一个路由,当访问 `/new-page` 时,会渲染 `NewPage.vue` 组件。 然后,在你的按钮组件中使用 `router-link` 标签: ```html <template> <router-link to="/new-page">跳转到新页面</router-link> </template> ``` 这样,点击按钮就会跳转到 `/new-page` 页面。 如果你想使用 VuePress 搭建一个 Vue 风格的技术文档/博客,可以参考 VuePress 官方文档中的教程:[VuePress](https://vuepress.vuejs.org/)。首先,你需要安装 VuePress: ``` npm install -D vuepress ``` 然后,在你的项目中创建一个 `.vuepress` 目录,并在其中创建一个 `config.js` 文件: ```javascript module.exports = { title: 'My Blog', description: 'My awesome blog built with VuePress', themeConfig: { nav: [ { text: 'Home', link: '/' }, { text: 'Guide', link: '/guide/' }, { text: 'About', link: '/about/' } ], sidebar: [ '/', '/guide/', '/about/' ] } } ``` 这里定义了站点的标题、描述、导航栏和侧边栏。接着,在 `.vuepress` 目录中创建一个 `README.md` 文件作为首页,以及其他的 Markdown 文件作为文章。 最后,你可以使用以下命令启动 VuePress: ``` npx vuepress dev ``` 这样,你就可以在浏览器中访问你的博客了。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值