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

window.location.href = 'some_url'

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


当前回答

从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)
});

);

其他回答

<div id="app">
   <a :href="path" />Link</a>
</div>

<script>
      new Vue({
        el: '#app',
        data: {
          path: 'https://www.google.com/'
        }
      });
</script> enter code here

只是一个非常简单的路由:

this.$router.push('Home')

我使用Vue3,这对我来说是有效的

router.push({ name: 'myRoute' })

如果你想路由到另一个页面,使用这个

this.$router.push({path: '/pagename'});

如果你想路由到另一个页面的参数使用这个

this.$router.push({
  path: '/pagename', 
  params: {
    param1: 'value1', 
    param2: 'value2'
  }
});

如果你正在使用vue-router,你应该使用router.go(path)来导航到任何特定的路由。路由器可以在组件中使用。$router来访问。

否则,window.location.href = 'some url';适用于非单页应用程序。

编辑:router.go()在VueJS 2.0中更改。你可以使用$router。Push ({name: "yourroutename"})或者只是router.push("yourroutename")现在重定向。

文档

注意:在控制器中使用:this.$router。Push ({name: 'routename'})