本文介绍了Vue2 使用 location.href 导航到外部 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 router.go、router.push、router.replace 和 window.location.href 转到www.mytargeturl.org"来重定向我的 vuejs 应用程序,但我总是得到 myVueapp.com/www.mytargeturl.组织这是我的路线:

I tried to go to 'www.mytargeturl.org' using router.go, router.push, router.replace and window.location.href to redirect my vuejs app but i always get myVueapp.com/www.mytargeturl.orgHere's my route:

  routes:[
{path: '/', component: App,
  children:[
    {
      path: 'detail',
      component: ItemDetail,
      props: true
    },
    {
      path: 'search',
      component: Middle
    }
  ]
},
{
  path: '/test', component: Test
},
{ path: '/a', redirect: 'www.mytargeturl.org' } // also tried this but didnt work

]

推荐答案

同意其他评论中的人的意见.Vue 的哲学不是解决已经解决的问题.同样在这里.只要有可能,只需为链接使用一个普通的 a 标签.如果您需要通过路由器,请使用 Navigation Guards:

Agreed with the people in other comments. Vue's philosophy is not to solve already solved problems. Same here. Just use an ordinary a tag for the link whenever possible. If you need to go through the router though, use Navigation Guards:

{
    path: '/redirect',
    beforeEnter(to, from, next) {
        // Put the full page URL including the protocol http(s) below
        window.location.replace("https://example.com")
    }
}

这篇关于Vue2 使用 location.href 导航到外部 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:18