Vue.js vue-simple-drawerを利用してメニューを横からスライド表示する

Vue.js vue-simple-drawerを利用してメニューを横からスライド表示する

vue.jsのライブラリである 「vue-simple-drawer」をインストールすると、 メニューを横からスライド表示させることができます。ここでは、 toggleボタンを押すと、画面左側からメニューを表示させるサンプルコードを記述してます。

環境

  • OS  CentOS 8.0.1905 (Core)
  • node v12.13.1
  • npm 6.13.2
  • @vue/cli 4.1.1

Vue環境構築

下記のコマンドで構築。今回は、vueappという名前でプロジェクトを作成してます。

vue create vueapp

プロジェクト作成時は、defaultを選択してます 。

Vue CLI v4.1.1? Please pick a preset: default (babel, eslint)

vue-simple-drawerインストール

下記の手順でインストールします。

※今回はbootstrap,sassも使用するので両方インストールします

## 移動
cd vueapp

## インストール
npm i -S vue-simple-drawer

## bootstarpも利用するのでインストール
npm i -S bootstrap-vue bootstrap
 
## sass-loaderも利用するのでインストール
npm i -S sass-loader node-sass

vue-simple-drawer使い方

src配下のApp.vueを下記のように編集します。

<template>
  <div id="app">
    <button @click="toggle" class="btn btn-success">toggle</button>
    <Drawer @close="toggle" align="left" :closeable="true">
      <div v-if="open">content here</div>
    </Drawer>
  </div>
</template>

<script>
import Drawer from "vue-simple-drawer";

export default {
  name: "App",
  components: {
    Drawer
  },
  data() {
    return {
      open: false
    }
  },
  methods: {
    toggle() {
      this.open = !this.open
    }
  }
};
</script>
<style lang="scss">
@import "~bootstrap/scss/bootstrap-reboot",
"~bootstrap/scss/buttons";
</style>
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  padding-top: 150px;
  margin: 0 auto;
  width: 800px;
}
</style>

起動します。

npm run serve

ブラウザから http://プライベートIP:8080 に アクセスすると、 toggleボタンを押すと、画面左側からメニュー が表示されることが確認できます。