英文:
In VueJs can i run a function after replacing the url?
问题
In VueJs, 我有以下函数:
domainLogin() {
const self = this;
const api = this.$store.state.api + "domainLogin";
axios
.post(api, {
registry: self.registry,
password: self.password
})
.then((response) => {
this.redirect(response.data.tenant)
self.$store.commit("auth", response.data.access_token)
})
.catch((error) => {
self.$message("Error", error.response, "error");
});
},
redirect(domain){
window.location.replace("https://" + domain + ".example.com.br")
},
我需要在重定向后存储 access_token,因为我需要将其保存在新域的本地存储中。我希望用户在重定向后已经登录到新域。
我正在重定向的域与主域位于同一应用程序中,唯一的不同之处是域名。这是否是正确的做法?如果是,应该如何操作?或者我应该尝试另一种方法?
英文:
In VueJs i have these functions:
domainLogin() {
const self = this;
const api = this.$store.state.api + "domainLogin";
axios
.post(api, {
registry: self.registry,
password: self.password
})
.then((response) => {
this.redirect(response.data.tenant)
self.$store.commit("auth", response.data.access_token)
})
.catch((error) => {
self.$message("Erro", error.response, "error");
});
},
redirect(domain){
window.location.replace("https://"+ domain +".example.com.br")
},
I need to store the access_token after the redirect because i need to save it in the new localStorage of the domain. I want this because i want the user to be redirected and already logged in the new domain.
The domain i'm redirecting is in the same application as the main domain, the only thing that changes is the domain.
Is this the way to do it? If so how? Or should i try another way?
答案1
得分: 2
I don't think you can do it this way, once the redirect
function executes, it will continue executing the other code in the scope.
一种解决方法是,您可以将 access token
作为查询参数发送到新域,并在那里使用它。
例如。
domainLogin() {
const self = this;
const api = this.$store.state.api + "domainLogin";
axios
.post(api, {
registry: self.registry,
password: self.password
})
.then((response) => {
this.redirect(response.data.tenant, response.data.access_token)
})
.catch((error) => {
self.$message("Error", error.response, "error");
});
},
redirect(domain, token){
window.location.replace("https://" + domain + ".example.com.br" + "?access_token=" + token)
},
英文:
I don't think you can do it this way, once the redirect
function executes, it will continue executing the other code in the scope.
One solution, you can send the access token
as a query parameter to the new domain, and use it there.
For example.
domainLogin() {
const self = this;
const api = this.$store.state.api + "domainLogin";
axios
.post(api, {
registry: self.registry,
password: self.password
})
.then((response) => {
this.redirect(response.data.tenant, response.data.access_token)
})
.catch((error) => {
self.$message("Erro", error.response, "error");
});
},
redirect(domain, token){
window.location.replace("https://"+ domain +".example.com.br"+"?access_token="
+ token)
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论