Vue函数式路由


如果你点击一个有样式的标签,或者是一个封装好的按钮组件,这些都不提供:to的接口的,此时就得用函数式路由了,比如这种

<template>
  <div>
      <mt-navbar v-model="selected" class="container"  @click.native="goRouter">
        <mt-tab-item id="head-nav1">新歌</mt-tab-item>
        <mt-tab-item id="head-nav2">排行</mt-tab-item>
        <mt-tab-item id="head-nav3">歌单</mt-tab-item>
        <mt-tab-item id="head-nav4">歌手</mt-tab-item>
        <mt-tab-item id="head-nav5">彩铃</mt-tab-item>
      </mt-navbar>
  </div>
</template>
<script type="es6">
  import { Navbar, TabItem } from 'mint-ui';
  export default {
    name:'head-nav',
    data(){
      return {
        selected:'head-nav1'
      }
    },
    components:{Navbar, TabItem},
    methods:{
      goRouter(){
        var navItem=this.selected.charAt(this.selected.length-1);
        switch(navItem){
          case '1':this.$router.push({path:'index'});break;
          case '2':this.$router.push({path:'rank'});break;
          case '3':this.$router.push({path:'plist'});break;
          case '4':this.$router.push({path:'singer'});break;
          case '5':this.$router.push({path:'ringtone'});break;
        }
      }
    }
  }
</script>



回到上一级可以这么写

<template>
  <div class="rank-head container" :style="style">
    <a class="rank-head-back" @click="routerBack"></a>
    {{title}}
  </div>
</template>
<script type="es6">
  export default {
    props:['title','style'],
    name:'rank-head',
    methods:{
      routerBack(){
        this.$router.go(-1);
      }
    }
  }
</script>


blob.png


声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
WordPress 修改 RESTful API 的请求和响应
Node.js 使用 Jest 做单元测试
如何使主题支持 Woocommerce
River 主题
WordPress 自定义文章类型
MySQL 批量插入数据时如何解决重复问题
WordPress 设置菜单
WordPress 语言文件