动态路由实现首先在router/index.js定义动态路由
{
path: '/redirect',
component: Layout,
hidden: true,
children: [
{
path: '/redirect/:path(.*)', // redirect是个动态路由 动态路由的配置方法,表示匹配零个或多个路由,比如路由为 /redirect 时,仍然能匹配到 redirect 组件。
component: () => import('@/views/common/redirect/index')
}
]
}
然后就是路由重定向组件redirect/index.vue:
<script>
export default {
created() {
const { params, query } = this.$route
const { path } = params
this.$router.replace({ path: '/' + path, query })
},
render: function(h) {
return h() // avoid warning message
}
}
</script>
在登录组件的代码如下:
监听路由的变化代码如下:
// 监听路由的变化
$route: {
handler: function(route) {
// redirect后面的部分就会变成query
const query = route.query;
console.log(query)
if (query) {
// 将redirect提取出来同时将其他的参数放入到otherQuery
this.redirect = query.redirect
this.otherQuery = this.getOtherQuery(query)
}
},
// immediate :在create的时候就会进行调用
immediate: true
}
首先绑定handleLogin函数
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{
{ $t('login.logIn') }}</el-button>
点击登录按钮触发:
handleLogin() {
// 表单验证后触发里面的回调
this.$refs.loginForm.validate(valid => {
// 表单验证成功
if (valid) {
// 初始化验证
this.initVerify = true
this.$nextTick(() => {
this.$refs.verify.show()
})
} else {
console.log('error submit!!')
return false
}
})
}