Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue2 navigate to external url with location.href

Tags:

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 

]

like image 703
codely Avatar asked Jun 16 '17 18:06

codely


People also ask

How do I redirect an external URL in Vue?

Suppose we have a /contact route in our vue app, when a user visits the /contact page we need to redirect them to an external url https://www.google.com/contact/ instead of the same domain redirect. To redirect to an external url in Vue, we can use the window. location. href property.

How do I redirect a page to my Vue router?

But what if you want to redirect programmatically, without using an anchor tag at all? If you're using Vue Router you'll push the new URL onto the router in order to do a redirect: this. $router. push('www.yoursite.com/blog') .

What is VUEX in VUE JS?

Vuex is a state management pattern + library for Vue. js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.


2 Answers

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")     } } 
like image 118
damienix Avatar answered Sep 24 '22 23:09

damienix


Found the solution. By adding http in my target url, all is well! Like this window.location = 'http://mytargeturl.org"

This seems to be universal javascript truth not just vue.

like image 27
codely Avatar answered Sep 22 '22 23:09

codely