admin管理员组文章数量:1335513
I have a Vue application and when a user visits a specific page, I want to auto-redirect to an external page. I am not sure how to do that.
For example:
http://localhost:3000/redirecting/?link=
- I know how to read the link parameter from the URL. So, I'll get this.
- Then I have user auth data in state and I can get it in puted properties.
- Now, based on the user object I can determine if the user is logged in or not.
- If he is not logged in, redirect to the login page. If user is logged in, redirect to the external link (This is the problem area).
Note that there is no click or anything. As soon as a user visits this page, the redirect should happen. Even better if we can show a message 'Redirecting...' for a second.
Update: I am using Nuxt/Vue.
I have a Vue application and when a user visits a specific page, I want to auto-redirect to an external page. I am not sure how to do that.
For example:
http://localhost:3000/redirecting/?link=http://externalsite.
- I know how to read the link parameter from the URL. So, I'll get this.
- Then I have user auth data in state and I can get it in puted properties.
- Now, based on the user object I can determine if the user is logged in or not.
- If he is not logged in, redirect to the login page. If user is logged in, redirect to the external link (This is the problem area).
Note that there is no click or anything. As soon as a user visits this page, the redirect should happen. Even better if we can show a message 'Redirecting...' for a second.
Update: I am using Nuxt/Vue.
Share edited Sep 7, 2018 at 4:21 asanas asked Sep 7, 2018 at 3:46 asanasasanas 4,28015 gold badges52 silver badges82 bronze badges3 Answers
Reset to default 3The custom routing inNuxt also worked. But I went with the following method. This also helped me sow the 'Redirecting...' message for a second before redirecting.
<template>
<div>
<p>Redirecting...</p>
</div>
</template>
<script>
export default {
mounted () {
const user = this.$store.state.auth.user
if (user !== null) {
if (this.$route.query.link) {
window.location.href = this.$route.query.link
} else {
window.location.href = '' // Some default link
}
} else {
window.location.href = '' // login page
}
}
}
</script>
you can do with Navigation Guards . you don't need to you any view file. just use route file
const router = new VueRouter({
routes: [
{
path: '/foo',
ponent: Foo,
beforeEnter: (to, from, next) => {
// check link param exit using route param method
if (link exit) {
// window.location.href = add redirtect url
} else {
next()
}
}
}
]
})
You will have to execute the check immediately after the page has loaded. Now, whatever your login algorithm maybe, you should definitely have a way to access this status on the front end somehow.
function getLoginStatus(){
//do something, get login status from server whatsoever
return status; //boolean
}
function redirect(){
const url = window.location.search.split("?link=")[1];
const externalurl = 'https://foo.';
//get url, parse, etc etc
//check login status
if(getLoginStatus()){
window.location.href = externalurl;
}
else {
window.location.href = 'login.html';
}
}
window.onload = function(){
redirect();
}
Now all you have to do is organize this so that it fits your vue code or whatever code structure you are employing. You can use route or something. I myself wouldn't even use Vue to handle this since this has nothing to do with the user interface.
本文标签: javascriptVueJS Automatically Redirect on Page LoadStack Overflow
版权声明:本文标题:javascript - VueJS: Automatically Redirect on Page Load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292345a2448042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论