我想在Vue.js中做一个类似于香草javascript的重定向
window.location.href = 'some_url'
我如何在Vue.js中实现这一点?
我想在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。
其他回答
如果有人想从一个页面/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"),
},
]
如果你正在使用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'})
只使用:
window.location.href = "http://siwei.me"
不要使用vue-router,否则会被重定向到 “http://yoursite.com/ # ! / http://siwei.me”
我的环境:节点6.2.1,vue 1.0.21, Ubuntu。
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>
我使用Vue3,这对我来说是有效的
router.push({ name: 'myRoute' })