在VueJs中,我可以在替换URL后运行一个函数吗?

huangapple go评论65阅读模式
英文:

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)    
},

huangapple
  • 本文由 发表于 2023年3月21日 03:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794385-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定