Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

router not pushing to route

console.log('menucompo1: ', menuComponent);
    routes.push({
      path: '/menu',
      name: 'menu',
      component: () =>
        import('../views/' + menuComponent + '.vue'),
    });

In my console I get: menucompo1: PageMenu

and views/PageMenu.vue exists

0 likes
4 replies
LaryAI's avatar
Level 58

It looks like the code is correctly pushing a new route to the router, but it's possible that the router is not being updated or reloaded to reflect the new route. Try calling the router's addRoutes method after pushing the new route:

console.log('menucompo1: ', menuComponent);
routes.push({
  path: '/menu',
  name: 'menu',
  component: () => import('../views/' + menuComponent + '.vue'),
});
router.addRoutes(routes);

This should add the new route to the router and allow it to be navigated to.

boyjarv's avatar

Here is my code

import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'

import store from '@/store/index';

Vue.use(VueRouter)


const routes = new Array<RouteConfig>();
let menuComponent = 'Menu';
console.log('CBA: ', store.state);
if(!store.state.courseContent) {
  console.log('loading');
  console.log('ABC1111: ', store.state);
  console.log('CBA: ', store.state.courseContent);
  // setTimeout(function() {
    console.log('here2:', store.state.courseContent);
    if (typeof store.state.courseContent.menu === 'object') {
      console.log('here2:', store.state.courseContent.menu._type);
      switch(store.state.courseContent.menu._type) {
        case 'tiles':
          menuComponent = 'Menu';
          break;
        case 'pages':
          menuComponent = 'PageMenu';
          break;
      }
    }
    console.log('menucompo1: ', menuComponent);
    routes.push({
      path: '/menu',
      name: 'menu',
      component: () =>
        import("../views/" + menuComponent + ".vue"),
    });
    if (typeof store.state.courseContent.topics === 'array') {
    store.state.courseContent.topics.forEach((topic: any) => {
      let component = topic._component.charAt(0).toUpperCase() + topic._component.substring(1);
      console.log('component: ', component);
      console.log('topic: ', topic._uri);
      console.log('SHOW SPLASH: ', store.state.showSplash);
      routes.push({
        path: topic._uri,
        name: topic._uri,
        component: () =>
          import("../views/" + component + ".vue"),      
      });
    });
  }
  // }, 5000);
}

  //
  



const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior() {
    if (document.getElementById('vertical') !== null) {
      document.getElementById('vertical')!.scrollIntoView();
    }
    if (document.getElementById('horizontal') !== null) {
      document.getElementById('horizontal')!.scrollIntoView();
    }

    //go to top of page!
    var elem = document.getElementById('app');
    if (elem) {
      setTimeout(() => {
        elem!.scrollTop = 0;
      }, 100);
    }

  }
});
if(store.state.courseContent) {
  console.log('store.state.started: ', store.state.started);
  if (store.state.courseContent.global._navigation === 'navigation' && !store.state.started) {
    Vue.nextTick(() => {
      router.replace({path: '/menu'});
    });
  }
}

export default router;

Please or to participate in this conversation.