Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
How to use Vue Router for scrolling navigation without loading in new components?

This project uses Vue 3 and Laravel 8.

I want to use vue-router for single page navigation without loading in new components and instead just using the router-links to scroll to sections similar to how anchor tags work:

  <router-link :to="{ hash: '#home' }">Home</router-link>

In Vue 2 I was able to do that by simply removing the component attribute from a route but in Vue 3 I am getting these errors in browser console when I do that:

[Vue Router warn]: Component "default" in record with path "/" is not a valid component. Received "undefined".

[Vue Router warn]: uncaught error during route navigation: app.js:40900

Error: Invalid route component
    at extractComponentsGuards (app.js:42803)
    at app.js:43926

[Vue Router warn]: Unexpected error when starting the router: Error: Invalid route component
    at extractComponentsGuards (app.js:42803)
    at app.js:43926

router.js:

import { createWebHistory, createRouter } from "vue-router";

import Home from "../vue/views/home";
import About from "../vue/views/about";


const router = createRouter({
    history: createWebHistory(),
    routes: [
        {path: "/", name: "home", component: Home},
        {path: "/about", name: "about", component: About},
    ],
    scrollBehavior(to, from, savedPosition) {
        if (to.hash) {
            return window.scrollTo({ top: document.querySelector(to.hash).offsetTop, behavior: 'smooth' });
        }
        return window.scrollTo({ top: 0, behavior: 'smooth' });
    }
});
export default router;

When I don't remove the component attribute from a route like so:

    {path: "/", name: "home"}

the home component is loaded in twice and overlaps either below the home component or is displayed at the end of the page.

How can I prevent that and just use the scrolling functionality?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda