Nuxt.js vue-slide-barを使用してスライダーコントロールを実装する
- 2020.02.14
- nuxt.js
- nuxt.js, vue-slide-bar

ライブラリ「vue-slide-bar」をインストールすると、スライダーコントロールの実装が簡単に可能です。ここでは、nuxt.jsで vue-slide-barを利用するための手順と簡単な使い方を記述してます。
環境
- OS ubuntu19.10
- node v12.13.0
- npm 6.13.7
- Nuxt.js v2.11.0
Nuxt.js環境構築
下記のコマンドで構築。ここでは、nuxtappという名前でプロジェクトを作成してます。
1 |
npx create-nuxt-app nuxtapp |
下記の設定で構築してます。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
✨ Generating Nuxt.js project in nuxtapp ? Project name nuxtapp ? Project description My laudable Nuxt.js project ? Author name taro ? Choose the package manager Yarn ? Choose UI framework None ? Choose custom server framework None (Recommended) ? Choose Nuxt.js modules (Press <space> to select, <a> to toggle all, <i> to invert selection) ? Choose linting tools ESLint ? Choose test framework None ? Choose rendering mode Universal (SSR) ? Choose development tools (Press <space> to select, <a> to toggle all, <i> to invert selection) yarn run v1.19.1 |
外部IP許可
localhostではなくプライベートIPを利用して接続して確認したいので、作成したプロジェクト配下にあるpackage.jsonに下記を追加します。
1 2 3 4 5 6 |
"config": { "nuxt": { "host": "0.0.0.0", "port": "3000" } }, |
vue-slide-barインストール
下記のコマンドでインストールします。
1 2 3 4 5 |
## 作成したプロジェクトに移動 cd nuxtapp ## インストール yarn add vue-slide-bar |
vue-slide-bar使い方
plugins配下にplugin.jsを作成し、下記の通りに編集します。
1 2 3 |
import Vue from 'vue' import VueSlideBar from 'vue-slide-bar' Vue.component('VueSlideBar', VueSlideBar) |
プロジェクト配下にあるnuxt.config.jsに下記のコードを追加します。
1 2 3 4 5 6 |
plugins: [ { src: '@/plugins/plugin', mode: 'client' } ], |
pages配下にあるindex.vueを下記の通りに編集します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
<template> <div class="container"> <div> <logo /> <div> <VueSlideBar v-model="value" /> </div> <div> <VueSlideBar v-model="slider.value" :data="slider.data" :range="slider.range" :label-styles="{ color: '#39B60A', backgroundColor: '#39B60A' }" :process-style="{ backgroundColor: '#39B60A' }" :tooltip-styles="{ backgroundColor: '#39B60A', borderColor: '#39B60A' }" @callbackRange="callbackRange" /> <h2>Value: {{ slider.value }}</h2> <h2>Label: {{ rangeValue.label }}</h2> </div> </div> </div> </template> <script> import Logo from '~/components/Logo.vue' export default { name: 'Nuxttest', components: { Logo }, data () { return { value: 50, rangeValue: {}, slider: { value: 45, data: [ 15, 30, 45, 60, 75, 90, 120 ], range: [ { label: '15 mins' }, { label: '30 mins', isHide: true }, { label: '45 mins' }, { label: '1 hr', isHide: true }, { label: '1 hr 15 mins' }, { label: '1 hr 30 mins', isHide: true }, { label: '2 hrs' } ] } } } } </script> <style> .container { margin: 0 auto; min-height: 100vh; display: flex; justify-content: center; align-items: center; text-align: center; } .image { width: 140px; height: 100px; background-size: cover; cursor: pointer; margin: 5px; border-radius: 3px; border: 1px solid lightgray; object-fit: contain; } a { color: #42b983; text-decoration: none; } h1 { font-size: 54px; color: #39B60A; margin: 30px 0 10px; } h2 { font-size: 22px; color: #555; } @media (max-width: 768px) { h1 { font-size: 30px; } h2 { font-size: 16px; } } </style> <style src="../node_modules/social-share.js/dist/css/share.min.css"></style> |
起動します
1 |
yarn dev |
ブラウザから http://プライベートIP:3000にアクセスすると、スライダーコントロールが実装されていることが確認できます。

-
前の記事
Nuxt.js timeline-vuejsを使用してタイムラインを表示する 2020.02.13
-
次の記事
Nuxt.js vue-highlight-wordsを使用して指定した文字列をハイライト表示する 2020.02.14
コメントを書く