Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda