Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

584
Views
TypeError: no se pueden leer las propiedades de undefined (leyendo '$ enrutador') vuejs

Entonces, estoy tratando de redirigir al usuario a una ruta diferente si la llamada api devuelve el estado 422. Pero recibo un error

 TypeError: Cannot read properties of undefined (reading '$router')

mis rutas.js:

 { path: '/dashboard', component: Dashboard, name: 'Dashboard', beforeEnter: (to, form, next) =>{ axios.get('/api/authenticated') .then(()=>{ next(); }).catch(()=>{ return next({ name: 'Login'}) }) }, children: [ { path: 'documentCollections', component: DocumentCollection, name: 'DocumentCollections' }, { path: 'document', component: Document, name: 'Document' }, { path: 'createDocument', component: CreateDocument, name: 'CreateDocument' }, { path: 'suppliers', component: Suppliers, name: 'Suppliers' }, { path: 'settings', component: Settings, name: 'Settings' }, ] }

También tengo componentes de inicio de sesión/registro y cuando uso

 this.$router.push({ name: "DocumentCollections"});

Redirige al usuario sin ningún error. El problema es cuando estoy en el componente secundario del componente del tablero.

en el componente documentCollections tengo un método:

 loadCollections(){ axios.get('/api/documentCollections') .then((response) => { this.Collections = response.data.data this.disableButtons(response.data.data); }) .catch(function (error){ if(error.response.status === 422){ //here is where the error happens this.$router.push({ name: "Settings"}); } }); },

Eso carga las colecciones, pero si el usuario tiene algún conjunto de datos, el estado de retorno de la API nula es 422. y quiero que sea redirigido al componente Configuración. (tanto documentCollection como Settings son componentes secundarios de Dashboard)

¿Por qué this.$router.push no funciona aquí pero funciona en el componente de inicio de sesión/registro?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Llamar a this dentro de una función de devolución de llamada crea un nuevo enlace a this objeto en lugar del objeto Vue en una expresión de función regular.

Puede usar la sintaxis de flecha para definir la función, por this no se sobrescribirá.

 .catch((error) => { if(error.response.status === 422){ this.$router.push({name: "Settings"}); } })

Más información

Otra opción

Defina otra instancia de this antes de la llamada axios y utilícela después de recibir la respuesta.

 let self = this ... self.$router.push({name: "Settings"})

con tu codigo

 loadCollections(){ let self = this; axios.get('/api/documentCollections') .then((response) => { this.Collections = response.data.data this.disableButtons(response.data.data); }) .catch(function (error){ if(error.response.status === 422){ self.$router.push({name: "Settings"}); } }); },
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!