2017-06-16 83 views
1

我試圖去'www.mytargeturl.org'使用router.go,router.push,router.replace和window.location.href重定向我的vuejs應用程序,但我總是GET myVueapp.com/www.mytargeturl.org 這裏是我的路線:Vue2導航到外部URL與位置.href

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 

]

+0

只需使用常規的'a'標籤。 vue-router用於在您的應用程序內導航 –

+0

很簡單:不要使用vue-router,因爲它會重定向到內部鏈接(即在應用程序內)。使用'window.location',這應該工作得很好。 – Terry

+0

我在發佈之前已經嘗試過window.location,但沒有成功。這裏是我的示例:https://jsfiddle.net/alvirtuoso/dkb1d13h/ – codely

回答

1

同意與人的意見。 Vue的哲學不是解決已經解決的問題。同樣在這裏。只要有可能,只需使用通常的a標籤即可。如果您需要通過路由器,請使用Navigation Guards

{ 
    path: '/a', 
    ... 
    beforeEnter: (to, from, next) => { 
     window.location = "http://www.mytargeturl.org" 
    } 
} 
+0

這仍然指引我在內部像myvueapp.com/www.mytargeturl.org – codely

+0

哦,我很抱歉,您需要使用協議的完整頁面url。 https://www.google.com – damienix

3

找到解決方案。在我的目標網址中加入http,一切都很好!像這樣 window.location = 'http://mytargeturl.org"

這似乎是通用的JavaScript的真相不只是vue。