Vue.js redirection to another page

守給你的承諾、 提交于 2019-11-30 06:17:39

问题


I'd like to make a redirection in Vue.js similar to the vanilla javascript

window.location.href = 'some_url'

How could I achieve this in Vue.js?


回答1:


If you are using vue-router, you should use router.go(path) to navigate to any particular route. The router can be accessed from within a component using this.$router.

Otherwise, window.location.href = 'some url'; works fine for non single-page apps.

EDIT: router.go() changed in VueJS 2.0. You can use router.push({ name: "yourroutename"}) or just router.push("yourroutename") now to redirect.

https://router.vuejs.org/guide/essentials/named-routes.html




回答2:


just use:

window.location.href = "http://siwei.me"

Don't use vue-router, otherwise you will be redirected to "http://yoursite.com/#!/http://siwei.me"

my environment: node 6.2.1, vue 1.0.21, Ubuntu.




回答3:


When inside a component script tag you can use the router and do something like this

this.$router.push('/url-path')



回答4:


Just a dead simple routing:

this.$router.push('Home');



回答5:


can try this too ...

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



回答6:


window.location = url;

'url' is the web url you want to redirect.




回答7:


You can also use the v-bind directly to the template like ...

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

the : is the abbreviation of v-bind.




回答8:


<div id="app">
   <a :href="path" />Link</a>
</div>

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



回答9:


If anyone wants permanent redirection from one page /a to another page /b


We can use redirect: option added in the router.js. For example if we want to redirect the users always to a separate page when he types the root or base 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"),

    },
   ]



回答10:


if you want to route to another page this.$router.push({path: '/pagename'})

if you want to route with params this.$router.push({path: '/pagename', param: {param1: 'value1', param2: value2})



来源:https://stackoverflow.com/questions/35664550/vue-js-redirection-to-another-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!