iview 在导航里面加徽标数

效果图:

     

代码如下:

思路:

 1.首先要改变menuList,
2.从后台获取,而且这个数据要要变化的,这时我们用watch来监听它。


<side-menu accordion :active-name="$route.name" :collapsed="collapsed" @on-select="turnToPage" :menu-list="menuList"> <!-- 需要放在菜单上面的内容,如Logo,写在side-menu标签内部,如下 --> <div class="logo-con" @click="returnHome"> <img v-show="!collapsed" :src="maxLogo" key="max-logo" style="cursor: pointer;"/> <img v-show="collapsed" :src="minLogo" key="min-logo" style="cursor: pointer;"/> </div> </side-menu>

 

js:

  1 <script>
  2   import sideMenu from './components/side-menu'
  3   import headerBar from './components/header-bar'
  4   import tagsNav from './components/tags-nav'
  5   import user from './components/user'
  6   import {mapMutations, mapActions} from 'vuex'
  7   import {getNewTagList, getNextName, getUserInfo} from '@/libs/util'
  8   import {getLeadsData} from '@/api/data'
  9   import minLogo from '@/assets/images/logo-min.svg'
 10   import maxLogo from '@/assets/images/logo.svg'
 11   import './main.less'
 12   export default {
 13     name: 'Main',
 14     components: {
 15       sideMenu,
 16       headerBar,
 17       tagsNav,
 18       user
 19     },
 20     data() {
 21       return {
 22         collapsed: false,
 23         userName: '',
 24         minLogo,
 25         maxLogo,
 26         allocateCount:'',
 27         trackCount:'',
 28         falseData:0,
 29         menuList:[]
 30       }
 31     },
 32     created() {
 33       let userName = getUserInfo()
 34       this.userName = userName.name;
 35       getLeadsData({}).then(res=>{
 36         this.allocateCount = res.data.allocateCount;
 37         this.trackCount = res.data.trackCount;
 38         this.falseData = 1;
 39       })
 40     },
 41     computed: {
 42       tagNavList() {
 43         return this.$store.state.app.tagNavList
 44       },
 45       tagRouter() {
 46         return this.$store.state.app.tagRouter
 47       },
 48       userAvator() {
 49         return this.$store.state.user.avatorImgPath
 50       },
 51       cacheList() {
 52         return this.tagNavList.length ? this.tagNavList.filter(item => !(item.meta && item.meta.notCache)) : []
 53       }
 54     },
 55     methods: {
 56       ...mapMutations([
 57         'setBreadCrumb',
 58         'setTagNavList',
 59         'addTag',
 60         'getUserInfo'
 61       ]),
 62       ...mapActions([
 63         'handleLogin'
 64       ]),
 65       turnToPage(name) {
 66         this.$router.push({
 67           name: name
 68         })
 69       },
 70       handleCollapsedChange(state) {
 71         this.collapsed = state
 72       },
 73       handleCloseTag(res, type, name) {
 74         const nextName = getNextName(this.tagNavList, name)
 75         this.setTagNavList(res)
 76         if (type === 'all') this.turnToPage('home')
 77         else if (this.$route.name === name) this.$router.push({name: nextName})
 78       },
 79       handleClick(item) {
 80         this.turnToPage(item.name)
 81       },
 82       //返回到首页
 83       returnHome(){
 84         this.$router.push({
 85           path: '/home'
 86         })
 87       }
 88     },
89 watch: { 90 '$route'(newRoute) { 91 this.setBreadCrumb(newRoute.matched) 92 this.setTagNavList(getNewTagList(this.tagNavList, newRoute)) 93 }, 94 falseData(){ 95 let info = this.$store.getters.menuList 96 let oldMenu = getUserInfo().menuNames 97 let arr = [] 98 info.forEach((a)=>{ 99 if(oldMenu.indexOf(a.meta.title) !==-1 ){ 100 arr.push(a) 101 } 102 }) 103 if(arr.length !== 0){ 104 for(let i=0;i<arr.length;i++){ 105 if(arr[i].children.length !== 0){ 106 let childArr = []; 107 arr[i].children.forEach((b)=>{ 108 if(oldMenu.indexOf(b.meta.title) !==-1 ){ 109 if(b.meta.title === 'Leads跟踪'){ 110 b.meta.badge = this.trackCount 111 } 112 if(b.meta.title === 'Leads分配'){ 113 b.meta.badge = this.allocateCount 114 } 115 childArr.push(b) 116 } 117 }) 118 arr[i].children = childArr 119 } 120 } 121 } 122 this.menuList = arr 123 } 124 }, 125 mounted() { 126 /** 127 * @description 初始化设置面包屑导航和标签导航 128 */ 129 this.setTagNavList() 130 this.addTag(this.$store.state.app.homeRoute) 131 this.setBreadCrumb(this.$route.matched) 132 } 133 } 134 </script>

 

posted @ 2018-07-28 17:51  粉色年华  阅读(2149)  评论(0编辑  收藏  举报