Nuxt.js vue-tabs-with-active-lineを使用してtabを実装する

ライブラリ「vue-tabs-with-active-line」をインストールすると、WEBサイトで、よく利用されるtab機能を利用することができます。ここでは、nuxt.jsで vue-tabs-with-active-lineを利用するための手順と簡単な使い方を記述してます。
環境
- OS ubuntu19.10
- node v12.13.0
- npm 6.13.7
- Nuxt.js v2.11.0
Nuxt.js環境構築
下記のコマンドで構築。ここでは、nuxtappという名前でプロジェクトを作成してます。
npx create-nuxt-app nuxtapp
下記の設定で構築してます。
✨ 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に下記を追加します。
"config": {
"nuxt": {
"host": "0.0.0.0",
"port": "3000"
}
},
vue-tabs-with-active-lineインストール
下記のコマンドでインストールします。
## 作成したプロジェクトに移動
cd nuxtapp
## インストール
yarn add vue-tabs-with-active-line
## sass-loaderも利用するのでインストール
yarn add sass-loader node-sass
vue-tabs-with-active-line使い方
plugins配下にplugin.jsを作成し、下記の通りに編集します。
import Vue from 'vue'
import Tabs from 'vue-tabs-with-active-line'
Vue.component('tabs', Tabs)
プロジェクト配下にあるnuxt.config.jsに下記のコードを追加します。
plugins: [
{
src: '@/plugins/plugin',
mode: 'client'
}
],
pages配下にあるindex.vueを下記の通りに編集します。
<template>
<div class="container">
<div>
<logo />
<tabs
:tabs="tabs"
:current-tab="currentTab"
:wrapper-class="'default-tabs'"
:tab-class="'default-tabs__item'"
:tab-active-class="'default-tabs__item_active'"
:line-class="'default-tabs__active-line'"
@onClick="handleClick"
/>
<div class="content">
<div v-if="currentTab === 'tab1'">
mebee.info
</div>
<div v-if="currentTab === 'tab2'">
message
</div>
<div v-if="currentTab === 'tab3'">
text
</div>
</div>
</div>
</div>
</template>
<script>
import Logo from '~/components/Logo.vue'
export default {
name: 'Nuxttest',
components: {
Logo
},
data () {
return {
tabs: [
{ title: 'Tab 1', value: 'tab1' },
{ title: 'Tab 2', value: 'tab2' },
{ title: 'Tab 3', value: 'tab3' }
],
currentTab: 'tab1'
}
},
methods: {
handleClick (newTab) {
this.currentTab = newTab
}
}
}
</script>
<style>
.container {
margin: 0 auto;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
</style>
<style lang="scss">
.default-tabs {
position: relative;
margin: 0 auto;
&__item {
display: inline-block;
margin: 0 5px;
padding: 10px;
padding-bottom: 8px;
font-size: 16px;
letter-spacing: 0.8px;
color: gray;
text-decoration: none;
border: none;
background-color: transparent;
border-bottom: 2px solid transparent;
cursor: pointer;
transition: all 0.25s;
&_active {
color: black;
}
&:hover {
border-bottom: 2px solid gray;
color: black;
}
&:focus {
outline: none;
border-bottom: 2px solid gray;
color: black;
}
&:first-child {
margin-left: 0;
}
&:last-child {
margin-right: 0;
}
}
&__active-line {
position: absolute;
bottom: 0;
left: 0;
height: 2px;
background-color: black;
transition: transform 0.4s ease, width 0.4s ease;
}
}
.content {
margin-top: 30px;
font-size: 20px;
}
</style>
起動します
yarn dev
ブラウザから http://プライベートIP:3000にアクセスすると、tab機能が実装されていることが確認できます。

-
前の記事
Vue.js vue-waitを使用してloading中を表示する 2020.03.06
-
次の記事
Pythonファイルをexe化する 2020.03.07
コメントを書く