Jim*_*bor 100 url-routing vue.js vue-router vuejs2
我想在Vue.js类似于vanilla javascript中进行重定向
window.location.href = 'some_url'
Run Code Online (Sandbox Code Playgroud)
我怎么能在Vue.js中实现这一点?
Jef*_*eff 141
如果您正在使用vue-router,则应使用router.go(path)导航到任何特定路线.可以使用组件内的组件访问路由器this.$router.
否则,window.location.href = 'some url';适用于非单页应用程序.
编辑:router.go()在VueJS 2.0中更改.您可以使用router.push({ name: "yourroutename"})或只是router.push("yourroutename")现在重定向.
https://router.vuejs.org/guide/essentials/named-routes.html
Dav*_*ano 69
根据文档,router.push似乎是首选方法:
要导航到其他URL,请使用router.push.此方法将新条目推送到历史堆栈中,因此当用户单击浏览器后退按钮时,它们将被带到先前的URL.
来源:https://router.vuejs.org/en/essentials/navigation.html
仅供参考:Webpack和组件设置,单页应用程序(通过Main.js导入路由器),我不得不通过以下方式调用路由器功能:
this.$router
Run Code Online (Sandbox Code Playgroud)
示例:如果要在满足条件后重定向到名为"Home"的路由:
this.$router.push('Home')
Run Code Online (Sandbox Code Playgroud)
Siw*_*申思维 33
只需使用:
window.location.href = "http://siwei.me"
Run Code Online (Sandbox Code Playgroud)
不要使用vue-router,否则你将被重定向到" http://yoursite.com/#!/http://siwei.me "
我的环境:节点6.2.1,vue 1.0.21,Ubuntu.
Nje*_*rus 20
在组件脚本标记内部,您可以使用路由器并执行类似的操作
this.$router.push('/url-path')
Run Code Online (Sandbox Code Playgroud)
Bag*_*zie 16
如果你想路由到另一个页面使用这个
this.$router.push({path: '/pagename'});
Run Code Online (Sandbox Code Playgroud)
如果你想用参数路由使用这个
this.$router.push({
path: '/pagename',
params: {
param1: 'value1',
param2: 'value2'
}
});
Run Code Online (Sandbox Code Playgroud)
所以,我所寻找的只是在哪里放置window.location.href,我得出的结论是,最好、最快的重定向方法是在路由中(这样,我们在重定向之前就不会等待任何内容加载)。
像这样:
routes: [
{
path: "/",
name: "ExampleRoot",
component: exampleComponent,
meta: {
title: "_exampleTitle"
},
beforeEnter: () => {
window.location.href = 'https://www.myurl.io';
}
}]
Run Code Online (Sandbox Code Playgroud)
也许它会对某人有所帮助..
You can also use the v-bind directly to the template like ...
<a :href="'/path-to-route/' + IdVariable">
Run Code Online (Sandbox Code Playgroud)
the : is the abbreviation of v-bind.
如果有人想从一个页面永久重定向/a到另一个页面/b
我们可以使用redirect:在router.js. 例如,如果我们想在用户键入根或基本 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"),
},
]
Run Code Online (Sandbox Code Playgroud)
小智 5
this.$router.push 可用于在 vue.js 中重定向页面
this.$router.push(url);
Run Code Online (Sandbox Code Playgroud)
示例 this.$router.push('home'); 该方法不会刷新页面
| 归档时间: |
|
| 查看次数: |
200401 次 |
| 最近记录: |