我想在Vue.js中做一个类似于香草javascript的重定向
window.location.href = 'some_url'
我如何在Vue.js中实现这一点?
我想在Vue.js中做一个类似于香草javascript的重定向
window.location.href = 'some_url'
我如何在Vue.js中实现这一点?
当前回答
我使用Vue3,这对我来说是有效的
router.push({ name: 'myRoute' })
其他回答
如果您正在使用Composition API,方法是使用类似这样的东西
<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()
router.push({ name: 'about' })
</script>
您可以使用Vue devtools检查路由的名称
我也建议使用name,因为如果你想要更新某个路径,你不需要重写所有的按钮/链接。
只使用:
window.location.href = "http://siwei.me"
不要使用vue-router,否则会被重定向到 “http://yoursite.com/ # ! / http://siwei.me”
我的环境:节点6.2.1,vue 1.0.21, Ubuntu。
在组件脚本标记中,您可以使用路由器并执行类似的操作
this.$router.push('/url-path')
<div id="app">
<a :href="path" />Link</a>
</div>
<script>
new Vue({
el: '#app',
data: {
path: 'https://www.google.com/'
}
});
</script> enter code here
也可以试试这个…
router.push({ name: 'myRoute' })