Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js redirection to another page

People also ask

How do I redirect to another page in Vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.

How do I push a route at Vue?

With Vue Router, you can use its router. push() function to programmatically navigate between routes on your site. You can either call push() with a string path, or with an object containing either the path or name of the route.

How do I redirect on Nextjs?

To redirect from / to another page with Next. js, we can get the pathname property from the Router object and do the redirect if pathname is '/' . import React, { useEffect } from "react"; import Router from "next/router"; const Comp = () => { //...

What is router in VUE JS?

Vue Router helps link between the browser's URL/History and Vue's components allowing for certain paths to render whatever view is associated with it.


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.

Documentation

Note: In controllers use: this.$router.push({ name: 'routename' })


According to the docs, router.push seems like the preferred method:

To navigate to a different URL, use router.push. This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.

source: https://router.vuejs.org/en/essentials/navigation.html

FYI : Webpack & component setup, single page app (where you import the router through Main.js), I had to call the router functions by saying:

this.$router

Example: if you wanted to redirect to a route called "Home" after a condition is met:

this.$router.push('Home') 

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.


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

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