Vue.js breadstickを使用してをNotificationを表示するまでの手順

Vue.js breadstickを使用してをNotificationを表示するまでの手順

vue.jsのライブラリで、ToastタイプのNotificationが実装できる「breadstick」の導入手順と簡単な使い方。ここでは、bootstrapとsassも利用するためインストールしてます。

環境

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

※CentOS8にVue.jsの環境構築はこちら

Vue環境構築

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

vue create vueapp

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

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

breadstickインストール

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

## 移動
cd vueapp

## インストール
npm i -S breadstick animejs

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

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

breadstick使い方

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

import Vue from 'vue'
import App from './App.vue'
import { BreadstickBakery } from "breadstick";

Vue.config.productionTip = false
Vue.use(BreadstickBakery);

new Vue({
  render: h => h(App),
}).$mount('#app')

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

<template>
  <div id="app">
    <div>
      <img alt="Vue logo" src="./assets/logo.png">
    </div>
    <button @click="showNotification" class="btn btn-success">Notificationを表示</button>
  </div>
</template>

<script>

export default {
  name: 'app',
  mounted() {
    this.showNotification();
  },
  methods: {
    showNotification() {
      this.$breadstick.notify("トーストを表示", {
        position: "top-right"
      });
    }
  }
}
</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 に アクセスすると Notificationが実装されていることが確認できます。