vue组合API结合路由

由于setup中无法访问this,因此无法直接访问this.$routerthis.$route,我们使用useRoute和useRouter
useRoute的参数改变由watch监听获取
官网地址:https://next.router.vuejs.org/installation.html
示例:
路由文件:index.js

// index.js
  {
    path: '/routerapi',
    name: 'RouterApi',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/RouterApi.vue'),
    children:[
      {
        path:'',
        component: () => import(/* webpackChunkName: "about" */ '../views/MyPage.vue'),
        redirect:'/routerapi/page/1', //路由重定向
      },
      {
        path:'page/:id',
        component: () => import(/* webpackChunkName: "about" */ '../views/MyPage.vue'),
      },
      {
        path:'article',
        component: () => import(/* webpackChunkName: "about" */ '../views/MyArticle.vue'),
      },
    ],
  },

在页面中引入 <router-link to="/routerapi">路由Api</router-link>
RouterApi.vue文件

// RouterApi.vue文件
<template>
  <div>
    <div id="menu">
      <h5 v-for="item in pages" >
        <router-link  :to="'/routerapi/page/'+item.id">{{item.title}}</router-link>
        <br>
      </h5>
      <h5><hr></h5>
      <h5><router-link  :to="{path:'/routerapi/article',query:{name:'abc',keyword:'hello'}}">文章一</router-link></h5>
      <h5><button @click="$router.push({path:'/routerapi/article',query:{name:'xyz',keyword:'world'}})">文章二</button></h5>
    </div>
    <div id="content">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
export default {
  name: "RouterApi",
  data(){
    return{
      pages:[
        {id:1,title:'目录一'},
        {id:2,title:'目录二'},
        {id:3,title:'目录三'},
        {id:4,title:'目录四'},
        {id:5,title:'目录五'},
      ],
    }
  },
}
</script>

<style scoped lang="scss">
#menu{
  width: 30%;
  background: #F0FFF0;
  height: 300px;
  float: left;
}
#content{
  width: 70%;
  background: #cccccc;
  height: 300px;
  float: right;
}
a {
  font-weight: bold;
  color: #2c3e50;

&.router-link-exact-active {
   color: #42b983;
 }
}

</style>

MyPage.vue文件

// MyPage.vue文件
<template>
  <h1>mypage</h1>
  <h3>{{pid}}</h3>
  <h2>id:{{id}}</h2>
</template>

<script>
import {useRoute,useRouter,onBeforeRouteLeave} from 'vue-router';
import {ref, watch} from "vue";
export default {
  name: "MyPage",
  computed:{
    pid(){
      return this.$route.params.id
    },
  },
  setup(){
    const route = useRoute()
    const router = useRouter()
    let id = ref()
    watch(()=>route.params.id,(newid)=>{
       id.value = newid
      //异步请求服务器API查找用户信息
    },{immediate:true})
    //5秒后跳转
    // setTimeout(()=>{
    //   router.push({path:'/routerapi/article',query:{name:'888888',keyword:'666666'}})
    // },5000)
    onBeforeRouteLeave((to,from)=>{
      let answer = window.confirm(`你确定要从${from.fullPath}${to.fullPath}`);
      if (!answer) return false
    })
    return {
      id
    }
  },
}
</script>

<style scoped>

</style>

MyArticle.vue文件

// MyArticle.vue文件
<template>
  <h1>myarticle</h1>
  <h3>{{$route.query.name}}</h3>
  <h3>{{$route.query.keyword}}</h3>
</template>

<script>
import {useRoute,useRouter} from 'vue-router';
import {ref, watch} from "vue";
export default {
  name: "MyArticle",
  computed:{
    pid(){
      return this.$route.params.id
    },
  },
  setup(){
    const route = useRoute()
    const router = useRouter()
    let id = ref()
    watch(()=>route.params.id,(newid)=>{
      id.value = newid
      //异步请求服务器API查找用户信息
    },{immediate:true})
    console.log(route.query.name,route.query.keyword)
    return {
      id
    }

  },
}
</script>

<style scoped>

</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值