Javascript vue中的动态路由器路径验证
{path:'/institutedetails/:id',组件:require('./views/institutedetails/instdails.vue') 我想在路径上重定向之前验证idJavascript vue中的动态路由器路径验证,javascript,html,vue.js,Javascript,Html,Vue.js,{path:'/institutedetails/:id',组件:require('./views/institutedetails/instdails.vue') 我想在路径上重定向之前验证id {path:'/institute\u details/:id',组件:require('./views/institutedetails/instdestails.vue'), 方法:{ 验证数据(){ var re=new RegExp(“^[a-zA-Z][0-9]{6}$”) var str
{path:'/institute\u details/:id',组件:require('./views/institutedetails/instdestails.vue'),
方法:{
验证数据(){
var re=new RegExp(“^[a-zA-Z][0-9]{6}$”)
var string1=this.seatno
var vm=这个
if(this.seatno=''| | this.seatno.length==0){
这个。seatno=“”
警报(“输入您的座位号…!”)
这是。$refs.seatno1.focus();
}否则,如果(!重新测试(string1)){
这个。seatno=“”
警报(“无效…!”)
这是.$router.push({path:'/'})
}
}
}
视图路径
您可以在组件中使用beforeRouteEnter
挂钩
其用途在vue路由器官方文档中有详细说明
在组件中编写示例代码
export default {
data() {
// define your component's data here.
},
methods: {
validateDate() {
// function to valiate your data
},
},
beforeRouteEnter(to, from, next) {
// check before enter the route
// if check failed, you can cancel this routing
// fake code here
const isCheckPassed = checkWithSomeLogic();
if (isCheckPassed) {
next();
} else {
// cancel this routing
// or redirect to the page you want with next function
next({
path: 'route you want to redirect to',
query: {
// your query params here
},
});
}
},
};
希望有帮助。您可以在组件中使用
beforeRouteEnter
hook
其用途在vue路由器官方文档中有详细说明
在组件中编写示例代码
export default {
data() {
// define your component's data here.
},
methods: {
validateDate() {
// function to valiate your data
},
},
beforeRouteEnter(to, from, next) {
// check before enter the route
// if check failed, you can cancel this routing
// fake code here
const isCheckPassed = checkWithSomeLogic();
if (isCheckPassed) {
next();
} else {
// cancel this routing
// or redirect to the page you want with next function
next({
path: 'route you want to redirect to',
query: {
// your query params here
},
});
}
},
};
希望有帮助。如果您只想切换到特定的路由,并在语句
中的add条件上应用逻辑,如果(to.fullPath.includes('/yoururl'))
那么下一步()
如果您只想切换到特定路由,并在语句中的add条件上应用逻辑,如果(to.fullPath.includes('/yoururl'))
thennext()
hi im使用beforeRouteEnter(to,from,next){axios.get(eventBus.apirl+'result/'+this.seatno)。然后(函数(response){console.log('RES'+response.data.scc_number)})。catch(错误=>{alert('Invalid'))}),bt它显示errorUncaught TypeError:无法读取未定义组件数据的属性“seatno”,在beforeRouteEnter钩子中不可访问。如官方文件中所述:beforeRouteEnter(to,from,next){//在确认呈现此组件的路由之前调用。//无法访问“this”组件实例,//因为调用此防护时尚未创建它!},
或者如果您只想在路由之前检查数据。只需在validateData函数中将router link元素替换为编程导航即可。在validateData函数中,如果validate通过,则路由到所需的路径。如果未通过,则不执行任何操作或向用户提供提示(例如)。hi im使用beforeRouteEnter(to、from、next){axios.get(eventBus.apiURL+'result/'+this.seatno)。然后(函数(response){console.log('RES'+response.data.scc_number)})。catch(错误=>{alert('Invalid')},bt它显示errorUncaught TypeError:无法读取未定义组件的数据的属性“seatno”在beforeRouteEnter钩子中不可访问。如官方文件中所述:beforeRouteEnter(to,from,next){//在确认呈现此组件的路由之前调用。//无法访问“this”组件实例,//因为调用此防护时尚未创建它!},
或者如果您只想在路由之前检查数据。只需在validateData函数中将router link元素替换为编程导航即可。在validateData函数中,如果validate通过,则路由到所需的路径。如果未通过,则不执行任何操作或向用户提供提示(例如)。