Vue2 navigate to external url with location.href

codely picture codely · Jun 16, 2017 · Viewed 35.7k times · Source

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.org Here'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

]

Answer

damienix picture damienix · Jun 16, 2017

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