Encountering difficulties in JavaScript while trying to instantiate Vue Router

After following the guide, I reached the point where creating a Vue instance was necessary (which seemed to work). However, it also required providing a Vue Router instance into the Vue constructor, as shown below.

const router = new VueRouter({ routes });
const app = new Vue({ router }).$mount('#app');

Unfortunately, this resulted in an error. Troubleshooting this issue has not yielded much information as encountered while google searching.

Uncaught ReferenceError: VueRouter is not defined(…)

I have verified that both packages are installed, along with some additional ones.

+-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdbbb8a88dffe3fde3f5">[email protected]</a>
+-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f7f4e4ace2ede8c1b3afb4afb0">[email protected]</a>
+-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c6a6979316e736968796e5c2e322c322f">[email protected]</a>

I have not implemented the importing for these two yet (uncertain about the placement in the code, and attempting with my index.js resulted in errors recognizing the token). However, I believe they may not be essential since Vue is recognized while only its router seems to cause issues. If importing is crucial, I assume both would fail if omitted.

import Vue from 'vue'
import VueRouter from 'vue-router'

The situation is further complicated because the decision was made to run it under Net.Core instead of NodeJs, which might pose limitations. We will not be using Webpack or Browserify to streamline the process (opting for plain dotnet run). Although this information may not be relevant at this stage, it's worth noting that what was supposed to be "so simple and easy" according to sources, appears more complex and cumbersome than anticipated. It seems straightforward in a specific environment but requires hands-on adjustments in my case.

What steps can I take to delve deeper into investigating this issue?

Answer №1

Here is a basic setup for vue-router. Remember to import Vue and vue-router so you can reference them in your code:

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use( VueRouter );

const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }

const routes = [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar }
]

const router = new VueRouter({
  routes
})

const app = new Vue({
  router
}).$mount('#app')
<div id="app">
  <h1>Hello App!</h1>
  <p>
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <router-view></router-view>
</div>

Make sure your imports are set up correctly for this to work, as it has been reported to be challenging.

If you prefer not to use imports, you can link directly to the libraries in your HTML and remove the import lines, like so:

Vue.use( VueRouter );

const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }

const routes = [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar }
]

const router = new VueRouter({
  routes
})

const app = new Vue({
  router
}).$mount('#app')
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0573706028776a7071607745372b">[email protected]</a>"></script>
<div id="app">
  <h1>Hello App!</h1>
  <p>
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <router-view></router-view>
</div>

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

Exploring i18nNext integration with antd table in React

Presently, this is the UI https://i.stack.imgur.com/CMvle.png App.tsx import "./styles.css"; import MyTable from "./MyTable"; export default function App() { const data = [ { key: "1", date: "2022-01- ...

Error message: The login buttons from Meteor's accounts-ui-bootstrap-3 are not showing up on the webpage

Following the installation of bootstrap-3 and accounts-ui-bootstrap-3, the expected ui-accounts login widget was not displayed when using {{ loginButtons }}. Instead, a <div> appeared in place of the widget, with no actual widget visible. Are there ...

Personalize Dropdown Menu, determining when to initiate the hide menu action

When creating a dropdown using ul and li tags, I am faced with the dilemma of determining the ideal time to hide the dropdown menu. The dropdown menu is designed to display values based on user input as they type, updating dynamically. Initially, I had se ...

Two states each offering a distinct perspective

I am currently working on modularizing my application using angular-ui-router to create a website with two states: main and checkout. In the main state, I want to have multiple "section" tags defined as ui-view items. However, it seems like there is an iss ...

Collaborating with SockJS connectivity

Currently, my Node.js backend is interacting with desktop clients using websockets. The communication from the server side is initiated from a web front-end and everything is functioning properly because I am storing the SockJS Connection instances in an ...

Tips for setting up a queue system for alert messages in Vue.js with Vuetify

Seeking assistance with modifying my code to handle multiple alerts and implement a customizable timeout duration. Any advice on how to approach this would be greatly appreciated. ~/store/toast-messages.js export const state = () => ({ color: '&a ...

Tips for displaying a Bootstrap 5 popover triggered by a select option change event

I'm using a select box with 4 options, and I have set it up so that when the user clicks on one of the options, a Bootstrap 5 popover is triggered dynamically upon the change event. Fiddle: https://jsfiddle.net/mayursutariya93/qjeg5r9b/6/ Here' ...

Error displayed inline

I am facing an issue with a hidden textbox that I need to make visible based on a certain condition. Despite checking that the control is being triggered by the change event, I am unable to get it to display. I have experimented with different methods with ...

Is it possible to forecast an on click event using jQuery?

I have encountered a specific issue. Within my code, there are certain elements, specifically <li> elements, that undergo a particular operation triggered by a click event: $('.panelTabs li').on('click', function () { // ..som ...

Verifying user login on NodeJS through connection from an IIS-hosted website

I am currently upgrading an outdated CMS system and looking to implement a real-time chat feature. The existing CMS operates on IIS, MSSQL, and PHP. The chat feature will be hosted on a separate Linux box running Node.js and Socket.io After successfully ...

Switch background color multiple times on click using JavaScript loop

Hello amazing people! I have a container with 52 small boxes and one large box containing a letter. The smaller boxes are arranged around the larger one using CSS grid, and when hovered over, they turn light grey. My Objective When any of the 52 small b ...

The Vue route parameters are not recognized within the function type

Seeking assistance on extracting parameters from my route in a vue page, I have the following implementation: <script lang="ts"> import { defineComponent } from 'vue'; import { useRoute } from 'vue-router'; export ...

Finding MongoDB data using an Express route and displaying it in a Jade template

Is there a way to retrieve data from MongoDB using an express route and display it in a jade template? Below is the code snippet of my express route (express version 2.5.8): app.get('/showData',function(req,res){ db.collection('comme ...

"Encountering an error when trying to access undefined property in templates

The content displayed in my component template is not what I expected when using @Output to pass an object from a parent container. While attempting to bind {{selectedMovDetail|json}} in the template, the output shows as { "name": "The Walking Dead","rati ...

Error in Node: JSON parse failure due to invalid token "'<'" and ""<!DOCTYPE ""

Every time I attempt to run node commands or create a new Angular project, I encounter the following error. Node version 20.11.0 NPM version 10.2.4 ...

Uncovering the Mystery Behind the Repetitive Execution of useEffect in Next.js

I am working on a Time Tracking feature for my Next.js application. In the ./app/components/TimeTracking.tsx file, I have implemented the following component: 'use client'; import React, { useEffect, useState } from 'react'; import { u ...

Using a Button component as a TableCell in a material-ui Table

Hey there! I'm looking for some assistance in adding buttons as TableRowColumns in the material-ui Table. I'm working on implementing an approval system to approve or reject user requests, and I thought presenting them in a tabular format would b ...

JavaScript API Response - conditional statement for handling a 'null' response

Does anyone have any suggestions for the following scenario: I have a response in .json format containing personal data of a person, who may or may not be assigned to a project. Here is an example response where the person is not assigned to a project: & ...

Elegant Bootstrap 4 Carousel featuring a glimpse of the upcoming slide alongside the primary carousel item

I am in search of a straightforward Bootstrap 4 carousel that showcases a glimpse of the next slide on the right. Despite exploring similar questions, I have not found a suitable solution. The links to those questions are: 1)Bootstrap carousel reveal part ...

Exploring the process of extracting a nested JSON value using Postman

I am currently facing an issue with parsing the json response from a post request, and then sending the parsed data to a put request. Here is the response body: { "createdBy": "student", "createdOn": "2019-06-18", "Id1": "0e8b9 ...