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

window.location.href = 'some_url'

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


当前回答

只使用:

window.location.href = "http://siwei.me"

不要使用vue-router,否则会被重定向到 “http://yoursite.com/ # ! / http://siwei.me”

我的环境:节点6.2.1,vue 1.0.21, Ubuntu。

其他回答

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

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

:是v-bind的缩写。

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

);

如果您正在使用Composition API,方法是使用类似这样的东西

<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()

router.push({ name: 'about' })
</script>

您可以使用Vue devtools检查路由的名称

我也建议使用name,因为如果你想要更新某个路径,你不需要重写所有的按钮/链接。

所以,我所寻找的只是在哪里放window.location.href和结论,我来的是,最好和最快的方式重定向是在路由(这样,我们不等待任何东西加载之前,我们重定向)。

是这样的:

routes: [
    {
        path: "/",
        name: "ExampleRoot",
        component: exampleComponent,
        meta: {
            title: "_exampleTitle"
        },
        beforeEnter: () => {
            window.location.href = 'https://www.myurl.io';
        }
    }]

也许它会帮助别人。

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

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

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

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