最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validate Routes in vue - Stack Overflow

programmeradmin0浏览0评论

I have set multiple routes on one page, and I want to validate only one route I am using the following code:

beforeRouteEnter(to, from, next) {
      
    
      if(firstString=='a'||firstString=='A'){
        if(parseInt(substring1) > 219028 && parseInt(substring1) < 386817){
  console.log("Valid")
      }
      else{
        alert("Invalid Seat No...!!")
          next({
            path: '/'
          });
          this.$refs.seatno1.focus();
        }

    },
<router-link :to="'institute_details/' + seatno " class="btn btn-success" >
                  <span v-on:click.capture="validateData">LINK</span>
                </router-link>



<router-link :to="'institute_details/' + agriculture"  data-toggle="tooltip" title="agri" data-placement="right" class="btn btn-danger">Agri</router-link> 

I have set multiple routes on one page, and I want to validate only one route I am using the following code:

beforeRouteEnter(to, from, next) {
      
    
      if(firstString=='a'||firstString=='A'){
        if(parseInt(substring1) > 219028 && parseInt(substring1) < 386817){
  console.log("Valid")
      }
      else{
        alert("Invalid Seat No...!!")
          next({
            path: '/'
          });
          this.$refs.seatno1.focus();
        }

    },
<router-link :to="'institute_details/' + seatno " class="btn btn-success" >
                  <span v-on:click.capture="validateData">LINK</span>
                </router-link>



<router-link :to="'institute_details/' + agriculture"  data-toggle="tooltip" title="agri" data-placement="right" class="btn btn-danger">Agri</router-link> 

I want to validate "'institute_details/' + seatno " only.

Share Improve this question edited Jan 16, 2019 at 15:26 Idris Dopico Peña 1751 gold badge3 silver badges16 bronze badges asked Apr 17, 2017 at 11:33 user5532713user5532713 1
  • Can i set to Parameter for particuler route? if yes then how i can set it? – user5532713 Commented Apr 18, 2017 at 5:27
Add a ment  | 

1 Answer 1

Reset to default 7

First, you can use regex / dynamic patterns in your route syntax to match relevant address. That should cover most use cases.

In this simple example you can recover the parameter inside the ponent in this.$route.params.location.

new VueRouter({
  routes: [
    { path: 'institute_details/:location', ponent: somePage }
  ]
})

For more details about it (such as advanced regex use) see: https://router.vuejs/guide/essentials/dynamic-matching.html

Additionnaly, as you did, you can use the navigation guard, either globally or on a ponent level, to do some fancier tests, and reject the navigation if it does not please you. Just call next(false).

beforeRouteEnter (to, from, next) {
    if (to.params.location === 'you-shall-not-pass') {
        next(false);
    }
    else {
        next();
    }
}

See: https://router.vuejs/guide/advanced/navigation-guards.html

发布评论

评论列表(0)

  1. 暂无评论