vue + element-ui 制作tab切換(適用於單頁切換不同標記顯示不同內容)


本篇文章使用vue結合element開發tab切換單頁不同的標記顯示不同的內容。

1、安裝element-ui

npm install element-ui --save

2、在main.js中引入element 和 css文件

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

//引入element-ui
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

//使用element-ui
Vue.use(ElementUI)
Vue.config.productionTip = false

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

3、編寫一個vue文件,這里我們命名為tabText.vue

a、使用element官方提供的api編寫template

<template>
    <div class="hello">
        
        <el-tabs v-model="activeName">
            <el-tab-pane label="tab1" name="first" :key="'first'">
                    <div style="background: yellow; display: inline">
                        tab1內容
                        tab1內容
                        tab1內容
                    </div>
            </el-tab-pane>

            <el-tab-pane label="tab2" name="second" :key="'second'">
                    <div style="background: green; display: inline">
                        tab2內容
                        tab2內容
                        tab2內容
                    </div>
            </el-tab-pane>
        </el-tabs>
        <!-- 注釋
       label:選項卡顯示的title
       name:與選項卡綁定的activeName對應的標識符,表示選項卡的別名
     --> </div> </template>


b、script部分

<script>
    export default {
        name: 'HelloWorld',
        data() {
            return {
                //默認第一個選項卡
                activeName: "first",
            }
        }
    }
</script>

//activeName:默認顯示那個tab。
//activeName:el-tabs組件雙向綁定的值,activeName值的取值是要對應el-tab-pane標記的name值

 4、成果展示

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM