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

window.location.href = 'some_url'

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


当前回答

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

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

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

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

其他回答

window.location = url;

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

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

);

为了与您最初的要求保持一致:

window.location.href = 'some_url'

你可以这样做:

<div @click="this.window.location='some_url'">
</div>

注意,这并没有利用Vue的路由器,但如果你想“在Vue.js中做一个类似于香草javascript的重定向”,这是可行的。

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

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

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

this.$router.push(url);

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