Vue2使用location.href导航到外部URL

cod*_*ely 15 vue.js vue-router vuejs2

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

  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
Run Code Online (Sandbox Code Playgroud)

]

dam*_*nix 22

在评论中同意人们的意见.Vue的理念不是解决已经解决的问题.同样在这里.a只要有可能,只需使用普通标签即可.如果您需要通过路由器,请使用导航保护

{
    path: '/a',
    beforeEnter(to, from, next) {
        // Put the full page url including the protocol http(s) below
        window.location = "http://example.com"
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这仍然在内部指导我,例如 myvueapp.com/www.mytargeturl.org (2认同)
  • 哦,对不起,您需要使用带有协议的整页网址,例如 https://www.google.com (2认同)

cod*_*ely 8

找到了解决方案.通过在我的目标网址中添加http,一切都很好!像这样 window.location = 'http://mytargeturl.org"

这似乎是普遍的javascript真相,而不仅仅是vue.


ego*_*ego 5

在这种情况下不需要使用导航守卫,更干净地在路由配置中使用动态重定向

routes:[
  {
    path: '/redirect-example',
    redirect: (to: Route) => {
      window.location.href = 'http://example.com'
      return '/redirecting' // not important since redirecting
    }
  }
]
Run Code Online (Sandbox Code Playgroud)