響應式小例子(vue+an-design-vue)

響應式小例子

記着玩,通過屏幕的寬度改變導航欄的位置
在這裏插入圖片描述
代碼如下:

<template>
  <div id='index'>
    <div :class="{left:isLeft,top:!isLeft}">
      <a-menu
        @click="handleClick"
        :defaultSelectedKeys="['1']"
        :openKeys.sync="openKeys"
        :mode="mode"
        :class="{left:isLeft,top:!isLeft}"
      >

        <a-sub-menu key="sub2" @titleClick="titleClick">
          <span slot="title"><a-icon type="appstore"/><span>子路由</span></span>
          <a-menu-item key="1">
            <router-link to="/index/foo">
              子路由Foo
            </router-link>
          </a-menu-item>
          <a-menu-item key="2">
            <router-link to="/index/bar">
              子路由bar
            </router-link>
          </a-menu-item>
          <a-sub-menu key="sub3" title="路由別名">
            <a-menu-item key="/bar" title="子路由bar別名,允許直接訪問,注意url是沒有index的">
              子路由bar別名
            </a-menu-item>
          </a-sub-menu>
        </a-sub-menu>

        <a-sub-menu key="sub4">
          <span slot="title"><a-icon type="setting"/><span>路由對比</span></span>
          <a-menu-item key="/my">
            子路由my,路由配置帶/
          </a-menu-item>
          <a-menu-item key="/index/hehe">
            子路由my別名,前面有index
          </a-menu-item>

        </a-sub-menu>
      </a-menu>
    </div>

    <div :class="[isLeft ? 'right' : 'bottom']">
      <!-- 路由出口 -->
      <!-- 路由匹配到的組件將渲染在這裏 -->
      <router-view></router-view>
    </div>
  </div>

</template>
<script>
  export default {
    name: 'index',
    data () {
      return {
        openKeys: ['sub2'],
        mode: 'inline',
        isLeft: true
      }
    },
    methods: {
      pushRouter (name) {
        this.$router.push({ path: name })
      },
      handleClick (e) {
        console.log('按鈕click', e)
        if (isNaN(e.key)) {
          this.$router.push({ path: e.key })
        }
      },
      titleClick (e) {
        console.log('titleClick', e)
      }
    },
    watch: {
      openKeys (val) {
        console.log('openKeys', val)
      }
    },
    // 鉤子函數
    mounted () {
      //獲取屏幕寬度設置樣式
      const that = this
      window.onresize = () => {
        return (() => {
          var width = window.screenWidth = document.body.clientWidth
          if (width <= 768) {
            that.mode = 'horizontal'
            that.isLeft = false
          } else {
            that.mode = 'inline'
            that.isLeft = true
          }
        })()
      }
    }
  }
</script>
<style lang='less' scoped>
  @leftWidth: 256px;
  @topHeight: 50px;
  #index {
    width: 100%;
    height: 100vh;

    .left {
      height: 100vh;
      width: @leftWidth;
      float: left;
    }
    .right {

      /*calc在less中計算出錯的解決方法*/
      width: calc(~"100%" - @leftWidth);
      padding: 20px;
      float: right;

    }

    .top {
      width: 100%;
      height: @topHeight;
    }

    .bottom {
      width: 100%;
      height: calc(~"100%" - @topHeight);
      padding: 10px;;
    }

  }
</style>

路由配置

alias: '/bar'這個別名使得可以通過
http://localhost:8080/bar訪問和http://localhost:8080/index/bar一樣的頁面。
同樣的,http://localhost:8080/myhttp://localhost:8080/index/hehe訪問相同的頁面

 {
      path: '/index',
      name: 'index',
      redirect: '/index/foo',
      component: () => import('./views/routerTest/index.vue'),
      children: [
        {
          // 當 /test/foo 匹配成功,
          // foo 會被渲染在 User 的 <router-view> 中
          path: 'foo',
          component: foo
        },
        {
          path: 'bar',
          component: bar,
          alias: '/bar' 
        },
        {
          path: '/my',
          component: require('@/components/routerTest/my').default,
          alias: 'hehe'
        }
      ]
    },
    ```

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章