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

window.location.href = 'some_url'

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


当前回答

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

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

其他回答

Vue3.js重定向

ve3 js全局重定向可以在main.ts中使用此方法定义

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)
app.config.globalProperties.$redirect = (page) => {router.push(page)}
app.use(router).mount('#app')

组件

<button @click="$redirect('/login')" type="button" class="btn ">Login</button>

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

this.$router.push(url);

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

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

router.push({ name: 'myRoute' })
<div id="app">
   <a :href="path" />Link</a>
</div>

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

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

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

:是v-bind的缩写。