如何在vuepress中设置多个侧边栏?

时间:2018-09-06 04:36:06

标签: vuepress

我的目录结构是这样的:

. ├─ README.md ├─ contact.md ├─ about.md ├─ foo/ | ├─ test/ | | ├─ README.md | | ├─ three.md | | └─ four.md │ ├─ README.md │ ├─ one.md │ └─ two.md └─ bar/ ├─ README.md └─ five.md

如何更新配置以为每个部分和文件夹定义边栏?

2 个答案:

答案 0 :(得分:1)

您可以在此处的文档中查看如何实现此目的的示例-https://vuepress.vuejs.org/theme/default-theme-config.html#multiple-sidebars

答案 1 :(得分:0)

您可以阅读完整的帖子here

设置

$ npm install -g vue-cli
$ vue init webpack <project-name>
$ npm install --save vuex gsap

Vuex模块

const types = {
  TOGGLE_SIDEBAR = 'TOGGLE_SIDEBAR' 
}

// initial state
const state = {
  sidebarOpen: false
}

// getters
const getters = {
  sidebarOpen: state => state.sidebarOpen
}

// actions
const actions = {
  toggleSidebar ({ commit, state }) {
    commit(types.TOGGLE_SIDEBAR)
  }
}

// mutations
const mutations = {
  [types.TOGGLE_SIDEBAR] (state) {
    state.sidebarOpen = !state.sidebarOpen
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}

商店

import Vue from 'vue'
import Vuex from 'vuex'
import ui from './modules/ui'

Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

export default new Vuex.Store({
  modules: {
    ui
  },
  strict: debug
})
  

您可以看到我们导入了UI模块,并将其作为模块添加到   导出的Vuex.Store对象。现在,我们要做的就是将其添加到   Vue实例。

import Vue from 'vue'
import App from './App'
import store from './store/index.js'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  template: '<App/>',
  components: { App }
})

组件(文件)...

APP.vue

<template>
  <div id="app">
    <div :class="$style.container">

    </div>
    <sidebar/>
    <sidebar-toggle/>
  </div>
</template>

<script>
import Sidebar from '@/components/sidebar.vue'
import SidebarToggle from '@/components/sidebarToggle.vue'
export default {
  name: 'app',
  components: {
    Sidebar, SidebarToggle
  }
}
</script>

<style>
  :root{
    --accent-color: #FFCB08;
    --primary-color: #820263;
    --dark-color: #2E294E;
  }

  *{
    box-sizing: border-box;
  }
</style>

<style module>
  .container{
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    width: 100vw;
    background-color: var(--primary-color);
  }
</style>

侧边栏切换

<template>
  <button :class="[open ? $style.active : '', $style.button]" @click="handleClick">    
    <svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
      <path d="M0 0h24v24H0z" fill="none"/>
      <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm5 11h-4v4h-2v-4H7v-2h4V7h2v4h4v2z"/>
    </svg>
  </button>
</template>

<script>
  import {TweenMax, Power4} from 'gsap'
  export default {
    name: 'sidebar-toggle',
    computed: {
      open () {
        return this.$store.state.ui.sidebarOpen
      }
    },
    methods: {
      handleClick () {
        this.$store.dispatch('toggleSidebar')
      }
    }
  }
</script>

侧边栏

<template>
  <div :class="$style.sidebar"/>
</template>

<script>
  import {TweenMax, Power4} from 'gsap'
  export default {
    name: 'sidebar',
    mounted () {
      TweenMax.set(this.$el, {
        x: this.$el.offsetWidth
      })
    },
    computed: {
      open () {
        return this.$store.state.ui.sidebarOpen
      }
    },
    watch: {
      open: function (open) {
        const dX = open ? 0 : this.$el.offsetWidth
        TweenMax.to(this.$el, 0.6, {
          x: dX,
          ease: Power4.easeOut
        })
      }
    }
  }
</script>

路线的演示结构

{//For Dropdown
"name":"Settings",
"route":undefined,
"icon":"/assets/icons/ic_settings_white_24px.svg",//"/assets/icons/ic_settings_black_24px.svg"
"children":[
    {
        "name":"Generate Code",
        "route":"settings/code"
    },
    {
        "name":"Subscription Layout",
        "route":"settings/layout"
    },
    {
        "name":"Plans",
        "route":"settings/plans"
    }
]
}

{//For Routes
    "name":"Settings",
    "route":"/settings",
    "icon":"/assets/icons/ic_settings_white_24px.svg",//"/assets/icons/ic_settings_black_24px.svg"
    "children":[]
}