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

Attempting to serialize a form using Ajax and jQuery

I am facing an issue with submitting user input from a modal using jQuery and AJAX. The problem is that the AJAX call is not capturing the user input. Even though the AJAX call is successful, when I check on the server side, the user input appears to be bl ...

D3: Ensuring Map is Scaled Correctly and Oriented Correctly

I am attempting to integrate a map into a website using D3 and topoJSON that resembles the following: However, when I create the map with D3/topoJSON, it shows up small and inverted. Even after exploring various solutions (like Center a map in d3 given a ...

Retrieving text data in Controller by utilizing jQuery AJAX request

Text box and button for input <input type="text" class="form-control" name="ClaimNumber" placeholder="Enter a claim number" id="ClaimNumber" /> <button class="btn btnNormal" type="submit" id="btnSearch"> ...

Building a dynamic form in React: Fetching select data from an API, posting to another API, and automatically clearing fields upon submission

I am currently working on a form that utilizes a GET request to retrieve data from an API endpoint and then proceeds to make a POST request to another endpoint. Although I have been successful in achieving this function, I am facing challenges with reset ...

How should .has(), .get() and .set() be properly implemented for a JavaScript Map() within an ExpressJS server?

Feeling thrown off by javascript Map()? My query revolves around javascript Map() - the set, get, and has() functions. Despite thinking I was an expert, it seems I still have things to learn... Situation: I'm dealing with a 'global' map sto ...

What is the best method for storing a Google Analytics object in a $_SESSION variable when transitioning between PHP scripts to avoid encountering a non-object error

Currently in the process of developing a web application that integrates with the Google Analytics API, however encountering challenges when it comes to implementation. The user is given the option to choose their profile from three interconnected drop-do ...

Utilizing VueJs components within Django with the help of django webpack loader: A step-by-step guide

After following a tutorial on integrating Django and VueJs using django-webpack-loader, I successfully loaded the main.js output from django-webpack-loader to my index.html. Here is the structure of my project directory: - assets - bundles - app. ...

Cypress .type is failing to properly update the v-model value in my Vue3-powered form

login.vue - this file represents the login page component <template> <v-container id="login-page"> <v-row> <v-col> <input id="username" v-model="username" data-cy="username&q ...

Utilizing Angular Directives and Controllers for Improved Efficiency

I have developed a custom dropdown option selector which is functioning well. It includes functions to fetch data from a specified URL in order to populate a list. However, the issue arises when I attempt to reuse this component in a different section of ...

Activating a function that requires locating a dynamically loaded element on the webpage using AJAX

I have a unique page on my website that allows users to make edits. Whenever they click on an item, the edit form is seamlessly loaded into a specialized section of the page using AJAX. Depending on the chosen item, the form fields are already prefilled w ...

Creating a dynamic multi-series line chart in D3 version 4 that incorporates data points with matching colors to their respective lines

In my attempt to enhance a multi-series line chart with D3 V4 in Angular-cli, I have encountered a challenge. Below is the code snippet of what I have been working on. var lookBookData = z.domain().map(function (name) { return { name: name, ...

Utilizing a search bar with the option to narrow down results by category

I want to develop a search page where users can enter a keyword and get a list of results, along with the option to filter by category if necessary. I have managed to make both the input field and radio buttons work separately, but not together. So, when s ...

Can a value be concealed within a dropdown list on a form (using PHP or JavaScript)?

Within my form, there is a drop down box listing the 50 states in the U.S. This list is generated using a PHP for loop. Users are required to select their residential state and later on, they must also choose a mailing state if it differs from their resi ...

Is there a pub/sub framework specifically designed for managing events in Angular?

Having a background in WPF with Prism, I am familiar with the IEventAggregator interface. It allows you to define events that can be subscribed to from controllers and then triggered by another controller. This method enables communication between controll ...

Tips for navigating to an external website through GraphQL

I am currently utilizing Node.js and Front-end on Next.js. I have a GraphQL server with a GetUrl method that returns a link (for example: ""). My goal is to redirect a client who made a request to that page with a Basic Auth Header. From what I understan ...

Please make sure to utilize messageCreate instead of the deprecated message event

Hey there, I'm currently in the process of upgrading my discord bot from v12 to v13. The bot is up and running, all commands are loaded in the console, but I'm facing an issue where a notification keeps popping up at the bottom and none of my com ...

To activate a function, simply click anywhere on the body: instruction

One of my latest projects involved creating a directive that allows users to click on a word and edit it in a text box. Once edited, clicking anywhere on the body should return the word back to its updated form. html <div markdown>bineesh</div& ...

Arrays nested in Laravel validator responses

Is there a way to customize the validator responses to expand nested arrays in Laravel? By default, Laravel uses the "dot notation" for error messages like this: [ 'organisation.name' => 'required|max:60|min:3', ...

Trouble arises when attempting to create a two-column layout using Material UI's <Grid> component

My goal is to create a two-column layout where each column takes up half of the screen and has equal height. To better illustrate, take a look at this image. The code I've tried so far is not working as expected: import React from "react"; import { ...

I am encountering a problem while attempting to fetch information from Firestore through the Firebase JS SDK

My current challenge revolves around retrieving data from Firestore using the Firebase JS SDK. A specific error message persists: An unexpected issue arises: TypeError: firebase_firestore__WEBPACK_IMPORTED_MODULE_3__.getDoc(...).data is not a function I ...