Vue.redirección de js a otra página


Me gustaría hacer una redirección en Vue.js similar al javascript de vainilla

window.location.href = 'some_url'

Cómo podría lograr esto en SDV.js?

Author: Saurabh, 2016-02-27

6 answers

Si está utilizando vue-router, debe usar router.go(path) para navegar a cualquier ruta en particular. Se puede acceder al router desde un componente usando this.$router.

De lo contrario, window.location.href = 'some url'; funciona bien para aplicaciones que no son de una sola página.

EDITAR: router.go() cambiado en VueJS 2.0. Puede usar router.push({ name: "yourroutename"}) o simplemente router.push("yourroutename") ahora para redirigir.

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

 67
Author: Jeff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-29 20:33:18

De acuerdo con los documentos, router.push parece ser el método preferido:

Para navegar a una URL diferente, use router.empujar. Este método empuja un nueva entrada en la pila de historial, por lo que cuando el usuario hace clic en el navegador botón atrás serán llevados a la URL anterior.

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

Para su información : Configuración de Webpack y componentes, aplicación de una sola página (donde importa el enrutador a través de Main.js), tuve que llame a las funciones del router diciendo:

this.$router

Ejemplo: si desea redirigir a una ruta llamada "Inicio" después de que se cumpla una condición:

this.$router.push('Home') 
 41
Author: Dave Sottimano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-01 19:33:33

Solo use:

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

No utilice vue-router, de lo contrario será redirigido a " http://yoursite.com/#! / http://siwei.me "

Mi entorno: nodo 6.2.1, vue 1.0.21, Ubuntu.

 15
Author: Siwei Shen申思维,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-23 05:11:02

Cuando está dentro de una etiqueta de script de componente, puede usar el enrutador y hacer algo como esto

this.$router.push('/url-path')
 5
Author: Njeru Cyrus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-25 17:22:09

Puede probar esto también ...

router.push({ name: 'myRoute' })
 1
Author: Davod Aslanifakor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-17 12:08:27
window.location = url;

'url' es la url web que desea redirigir.

 0
Author: Rayees Pk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-21 12:46:52