vue项目使用mint-ui框架,把tabbar封装为组件使用

喜你入骨 提交于 2019-12-06 04:55:35

在写程序的过程中参考了https://blog.csdn.net/wandoumm/article/details/80166414

 

目录结构如下,将tabbar封装成FooterBar组件

main.js引入mint-ui的组件,这里是全部引入

// 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'
import Mint from 'mint-ui'; //mint-ui的全部组件
import 'mint-ui/lib/style.css'

Vue.use(Mint) //使用MintUI
Vue.config.productionTip = false

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

组件FooterBar.vue

<template>
    <div>
        <mt-tabbar v-model="selected">
            <mt-tab-item id="index">
                首页
            </mt-tab-item>
            <mt-tab-item id="contents">
                文章
            </mt-tab-item>
            <mt-tab-item id="photo">
                相册
            </mt-tab-item>
            <mt-tab-item id="message">
                留言板
            </mt-tab-item>
            <mt-tab-item id="me">
                我的
            </mt-tab-item>
        </mt-tabbar>
    </div>
</template>

<script>
export default {
    name: 'footer-bar',
     props:{
        current: String,
     },
    data() {
        return {
            selected: this.current,
        }
    },
    watch: {
        selected: function (val, oldVal) {
            // 这里就可以通过 val 的值变更来确定去向
            switch(val){
                case 'index':
                    this.$router.push('/index');
                break;
                case 'contents':
                    this.$router.push('/contents');
                break;
                case 'photo':
                    this.$router.push('/photo');
                break;
                case 'message':
                    this.$router.push('/message');
                break;
                case 'me':
                    this.$router.push('/me');
                break;
            }
        }
    },
    
}
</script>

因为需要全局引用,所以写在App.vue中

App.vue

<template>
  <div id="app">
    <router-view/>
    <footer-bar></footer-bar>
  </div>
</template>

<script>

import FooterBar from './components/FooterBar'

export default {
  name: 'App',
  
  components: {
    'footer-bar': FooterBar,
  },
      
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>

index.js中把要跳转的页面配置好

import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/pages/Index'
import Contents from '@/pages/Contents'
import Photo from '@/pages/Photo'
import Message from '@/pages/Message'
import Me from '@/pages/Me'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index
    },
    {
      path: '/index',
      name: 'Index',
      component: Index
    },
    {
      path: '/contents',
      name: 'Contents',
      component: Contents
    },
    {
      path: '/photo',
      name: 'Photo',
      component: Photo
    },
    {
      path: '/message',
      name: 'Message',
      component: Message
    },
    {
      path: '/me',
      name: 'Me',
      component: Me
    }
  ]
})

首页,其他页面类似

<template>
    <div id="index" >首页</div>
</template>
<script>
export default {
    name: 'Index',
    data () {
      return {
          current: 'index'
        }
    },
}
</script>
<style>

</style>

全部写完后npm run dev运行即可

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!