[英]Validate Routes in vue
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. 我只想验证“'institute_details /'+ seatno”。
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 component in this.$route.params.location
. 在这个简单的示例中,您可以在
this.$route.params.location
恢复组件内部的参数。
new VueRouter({
routes: [
{ path: 'institute_details/:location', component: somePage }
]
})
For more details about it (such as advanced regex use) see: https://router.vuejs.org/en/essentials/dynamic-matching.html 有关它的更多详细信息(例如高级正则表达式的使用),请参见: https : //router.vuejs.org/en/essentials/dynamic-matching.html
Additionnaly, as you did, you can use the navigation guard, either globally or on a component level, to do some fancier tests, and reject the navigation if it does not please you. 此外,像您所做的那样,您可以全局或在组件级别使用导航保护功能,进行一些更高级的测试,如果不满意,请拒绝导航。 Just call
next(false)
. 只需调用
next(false)
。
beforeRouteEnter (to, from, next) {
if (to.params.location === 'you-shall-not-pass') {
next(false);
}
else {
next();
}
}
See: https://router.vuejs.org/en/advanced/navigation-guards.html 请参阅: https : //router.vuejs.org/en/advanced/navigation-guards.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.