What is the process for setting up a new router?

When it comes to templates, the vuestic-admin template from https://github.com/epicmaxco/vuestic-admin stands out as the perfect fit for my needs. However, I have a specific requirement to customize it by adding a new page without displaying it in the sidebar. To achieve this, I need to define a new router. The challenge I am facing is understanding how to add this new router.

Here is what I have done so far:

Firstly, I created a .vue file located at components/contact/Contact.vue. Here is its code:

<template>
  <div class="contact">
    <div class="row">
      <div class="col-md-12">
        <p>contact</p>
      </div>
    </div>
  </div>
</template>

<script>
  export default { name: 'contact' }
</script>

<style lang="scss"></style>

Secondly, I added a new .js file at store/modules/contact.js. The code is as follows:

import lazyLoading from './lazyLoading'

export default {
  name: 'Contact',
  path: '/contact',
  component: lazyLoading('contact/Contact'),

  meta: {
    default: false,
    title: 'menu.contact',
    iconClass: 'vuestic-icon vuestic-icon-extras'
  }
}

Thirdly, within the store/modules/menu/index.js file, I updated the state definition by including pages:

import contact from './contact'

const state = {
  pages: [
    contact
  ],

  items: [
    dashboard,
    statistics,
    forms,
    tables,
    ui,
    extra,
    auth
  ]
}

Fourthly, in the router/index.js file, I made the following changes:

export default new Router({
  routes: [
    ...generateRoutesFromMenu(menuModule.state.items),
    {path: '*', redirect: { name: getDefaultRoute(menuModule.state.items).name }},
    ...generateRoutesFromMenu(menuModule.state.pages),
    {path: '*', redirect: { name: getDefaultRoute(menuModule.state.pages).name }}
  ]
})

Upon compiling these changes, I encountered a console error:

Uncaught TypeError: Cannot read property 'name' of undefined

In my understanding (and assumption), the issue may lie in the fourth step.

I would greatly appreciate any guidance on resolving this problem. Thank you!

Answer №1

After reviewing the source code here for the function getDefaultRoute(),

function getDefaultRoute (menu = []) {
  let defaultRoute

  menu.forEach((item) => {
    if (item.meta.default) {
      defaultRoute = item
    } else if (item.children) {
      let defaultChild = item.children.find((i) => i.meta.default)
      defaultRoute = defaultChild || defaultRoute
    }
  })

  return defaultRoute
}

It appears that you can safely omit the line

{path: '*', redirect: { name: getDefaultRoute(menuModule.state.pages).name }}

This is because in the case of 'contact', the default: false is set and there are no children, resulting in a null return from the function (hence the error message you encountered).

The framework is designed to assume a single default menu item from a list. However, your current approach should still be valid.

The only potential concern would arise if you start modifying the core framework code, which could complicate future upgrades.


A note on future updates

Having performed a basic installation, I have realized that my previous comment about upgrade difficulties may not apply here.

Essentially, this project functions more as a template rather than a rigid framework. The installer generates starter code in your project's src directory, allowing you full flexibility to make modifications and save them in a separate GitHub repository.

There is no package within node_modules that would automatically override any router changes upon running npm install at a later date.

If Epicmaxco releases an updated version of the template that you wish to incorporate, you can simply create a new project and transfer the modifications you previously made in the src folder to the new project (consider using a diff tool between the old and new src folders).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is utilizing a standardized logging system considered a best practice for a node and express application?

I am currently working on a node.js application that consists of several dozen modules and uses bunyan for logging with JSON output and multiple configurable streams. I have been searching for good examples on how to implement a logger instance across all ...

Storing Firestore Timestamp as a Map in the database

Snippet Below const start = new Date(this.date + 'T' + this.time); console.log(start); // Thu Sep 12 2019 04:00:00 GMT+0200 const tournament:Tournament = { start: firebase.firestore.Timestamp.fromDate(start) } When passing the tournament ...

Encountering difficulties while trying to install ng2-material in Angular 2

I'm attempting to utilize a data table with the ng2-material library from ng2-material as shown below: <md-data-table [selectable]="true"> <thead> <tr md-data-table-header-selectable-row> <th class="md-text-cell">M ...

`validate.js verifying the elements within an array`

One of the challenges I'm facing in my JavaScript project is dealing with objects that have two array properties included. As part of my development process, I've decided to utilize the resources provided by the validate.js library. To illustrat ...

VuexFire Transformations

Can someone shed some light on the mysterious mutations: VuexFire.mutations line in this code snippet for Vuex v2? I'm a bit confused about where my actual mutations should go. Currently using Vuex v1 and everything seems fine, but considering an upgr ...

Refreshing an external file in Node.js at regular intervals

Seeking guidance on loading external files in node. I'm currently working with an external JSON file that houses some configuration settings, and this file gets updated by an outside process every 10 minutes. How can I automate the reloading of this ...

Unable to view the image in browsers other than Internet Explorer

On a webpage, there is a feature where clicking on the "Add More" link should display an input box and a "Delete" button image. Surprisingly, this functionality works perfectly on IE browsers, but not on Mozilla or Chrome. In non-IE browsers, only the text ...

Looking to set up an event handler for the browser's back button in next.js?

When my modal opens, it adds a hash to the URL like example.com/#modal. I want to be able to recognize when the back button is clicked in the browser so I can toggle the state of the modal. The challenge is that since I am using next.js (server-side rend ...

Sending information from a rails controller to a react component

Wondering how to pass the example @post = Post.all from the controller to React component props while integrating Rails with React via Webpacker. Is it necessary to do this through an API or is there another way? ...

Having trouble generating a readable output bundle due to the following error message: "The entry module cannot be found: Error: Unable to resolve './src/index.js'"

I'm currently working with Webpack version 6.14.8 within an Asp.net Core Razor Pages project in Visual Studio 2019. My objective is to generate a readable output file, and here's the directory structure I've set up: |-->wwwroot -----> ...

Utilizing web components from NPM packages in conjunction with Svelte

My current project involves the development of a simple Single Page App (SPA) using Svelte. I have successfully implemented a basic layout and styling, as well as an asynchronous web request triggered by a button click. Now, my next objective is to utiliz ...

Tips for prioritizing new data input at the top of the list

Hey there, I'm having trouble figuring out how to push new data to the top of a list using vue.js and laravel. I've been trying but haven't had any luck so far. If anyone could lend a hand, I would greatly appreciate it. Below is my Control ...

The usage of Arrow Functions within Object Literal Syntax

I can't seem to understand why an arrow function within an object literal is invoked with window as the context for this. Any insights on this would be greatly appreciated. var arrowObject = { name: 'arrowObject', printName: () => { ...

Retrieve Nuxt Router from plugin

I have developed a customized Nuxt plugin that includes some useful global functions loaded in the configuration file. However, I am facing a challenge as I try to access the router within one of these functions. Despite my efforts, I keep running into a ...

Expanding Lists in Bootstrap 3: A Step-by-Step Guide

I have been experimenting with Bootstrap's Example accordion code, which can be accessed via this link: http://jsfiddle.net/qba2xgh6/18/ <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> <div class="panel ...

How to Use Vue.js to Find the Nearest Div Element with a Specific

Below is the HTML code I am working with: <div id="app"> <div class="image"> <div class="overlay"> <p>Some overlay text</p> </div> <img src="https://placeimg.com/640/480/any" class="img-fluid"> ...

Is WebStorm with Node Supervisor being utilized to eliminate the need for restarting after every code modification?

Currently, I am utilizing WebStorm as my node IDE and have found it to be quite impressive. However, one issue I am facing is figuring out how to incorporate node supervisor into my workflow within WebStorm. Has anyone successfully managed to set this up ...

Discover a simple method for comparing JSON responses and UI elements by utilizing two arrays in a for loop within your Cypress automation tests

I am in need of assistance where I must compare JSON response data with UI elements. If matching elements are found, the task is to print them in a log file. This requires checking all JSON responses using a for loop. Can someone provide me with Cypress Ja ...

How do I retrieve and pass values from multiple inputs in my React application?

I've implemented the <autocomplete> as an input field, and I'm currently troubleshooting my submit method by logging values to the console. Unfortunately, I can only submit empty data to my axios instance without any Traceback errors for gu ...

The JavaScript function's if and else statements are being executed simultaneously

Upon loading the page, I am checking the value of a dropdown. Strangely, when I debug, it appears that the controller is executing both the if and else statements, which should not be happening. /* Here is my code: */ $(window).load(function() { ...