我想在Vue.js中做一个类似于香草javascript的重定向

window.location.href = 'some_url'

我如何在Vue.js中实现这一点?


当前回答

你也可以直接在模板上使用v-bind,就像…

<a :href="'/path-to-route/' + IdVariable">

:是v-bind的缩写。

其他回答

根据文件,是路由器。推送似乎是首选方法:

要导航到不同的URL,使用router.push。此方法将 历史堆栈中的新条目,因此当用户单击浏览器时 返回按钮,他们将被带到以前的URL。

来源:https://router.vuejs.org/en/essentials/navigation.html

供参考:Webpack和组件设置,单页应用程序(通过Main.js导入路由器),我必须调用路由器函数,这样说:

this.$router

示例:如果你想在满足条件后重定向到一个名为“Home”的路由:

this.$router.push('Home') 

美元。路由器。Push可以用来重定向vue.js中的页面

this.$router.push(url);

这个例子。美元router.push(“回家”); 此方法不会刷新页面

从api获取特定页面 最简单的方法是向URL添加另一个参数,该参数将破坏缓存。

router.replace(数据。电流+ '?t=' + (new Date())).getTime())

例如

router.beforeEach((to, from, next) => {

axios
    .get('http://domazin.com/currentpage')
    .then(response => {
     const data = response.data
       if (to.path != data.current) {
              router.replace(data.current + '?t=' + (new Date()).getTime())
       } else {
          next()
       }
    })
    .catch(error => {
        console.log(error)
});

);

在组件脚本标记中,您可以使用路由器并执行类似的操作

this.$router.push('/url-path')
window.location = url;

'url'是你想重定向的web url。