【應用】如何使用 element-ui 之 NavMenu 導航菜單 中 的 router 屬性(在激活導航時以 path 進行路由跳轉)


效果圖:

使用:

1.<template>

 

<el-header style="height: 61px" >    
  <el-menu
    mode = "horizontal"
    background-color = "#76c7f4"
    text-color = "#000000"
    active-text-color = "#ffffff"
    border-bottom = "none"
    router  // 啟用 router 屬性
    :default-active = "path"  // 當前激活的菜單綁定"path"
    class = "el-menu-demo">
    <template v-for="(item, index) in nav_menu_data">
        <el-menu-item :index = "item.path" :key = "index">
            {{item.title}}
          </el-menu-item>
    </template>
  </el-menu>
</el-header>
 

2.data

data () {
    return {
      path: '',
      nav_menu_data: [{
        title: '導航一',
        path: '/Menu/Page1'
      }, {
        title: '導航二',
        path: '/Menu/Page2'
      }, {
        title: '導航三',
        path: '/Menu/Page3'
      }, {
        title: '導航四',
        path: '/Menu/Page4'
      }]
    }
  }

3.created

created () {
    this.onRouteChanged()

4.methods

methods: {
    onRouteChanged () {
      let that = this
      that.path  = that.$route.path
    }
  }

5.watch

watch: {
    // 監測路由變化,只要變化了就調用獲取路由參數方法將數據存儲本組件即可
    '$route': 'onRouteChanged'
  }

6.style

<style scoped>
  .el-header {
    background-color: #76c7f4;
    display: flex;
    align-items: center;
    justify-content:center
  }
</style>

END

 

   unique-opened="true"

 


免責聲明!

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



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