我想在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。

其他回答

<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('/url-path')

如果有人想从一个页面/a永久重定向到另一个页面/b


我们可以使用在router.js中添加的redirect:选项。例如,当用户输入根或基url /时,我们想要将用户重定向到一个单独的页面:

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      redirect: "/someOtherPage",
      name: "home",
      component: Home,
      // () =>
      // import(/* webpackChunkName: "home" */ "./views/pageView/home.vue"),

    },
   ]

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

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

:是v-bind的缩写。

也可以试试这个…

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