我想在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' })
this.$router.push('/path')
,否则请返回已翻译的文本。 - Renier您还可以直接将 v-bind 应用到模板中,例如...
<a :href="'/path-to-route/' + IdVariable">
:
是 v-bind
的缩写。
/path-to-route/${IdVariable}
"。 - menepetthis.$router.push可以用于在Vue.js中重定向页面。
this.$router.push(url);
例如,这个方法 this.$router.push('home') 不会刷新页面。
<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()
router.push({ name: 'about' })
</script>
name
,因为如果有一天您想更新某个路径,您将不需要重写所有的按钮/链接。this.$router.push('about')
来引用名称自动地将其与Mohammad Mahdi KouchakYazdi的回答结合起来。 - undefined。
window.location.href = 'some_url'
你可以做类似于这样的事情:
<div @click="this.window.location='some_url'">
</div>
Vue3.js 重定向
在 main.ts 中,可以使用以下方法定义 Vue3 js 全局重定向。
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>
<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.$inertia.get(route('route.name', this.modelId));
从API获取特定页面 最简单的方法是向URL添加另一个参数,以打破缓存。
router.replace(data.current + '?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)
});
);