Unable to resolve the RouterLink tag

When setting up a Vanilla Vue CLI installation, I encountered some issues with getting the Vue router to work properly. Despite following the online documentation and forum discussions, I couldn't resolve the error message "Uncaught ReferenceError: router is not defined".

In my attempts to import the Vue router, I used the official method as described in the documentation -

import VueRouter from 'vue-router'
and Vue.use(VueRouter).

However, even after adding the router object, I continued to encounter errors, resulting in a "TypeError: Cannot read property 'resolve' of undefined" message during rendering.

// main.js

import 'bootstrap'
import 'bootstrap/dist/css/bootstrap.min.css'
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
//import axios from 'axios'
import { library } from '@fortawesome/fontawesome-svg-core'
import { faUserSecret } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

Vue.config.productionTip = false
Vue.component('font-awesome-icon', FontAwesomeIcon)

library.add(faUserSecret)

Vue.use(VueRouter)

export default new VueRouter({
  routes: [
    { path: '/', component: () => import('./views/Home.vue') },
    // { path: '/login', component: Login },
    // { path: '/about', component: About }
  ]
})

new Vue({
    render: h => h(App),
}).$mount('#app')
// App.vue

<template>
  <div id="app">

    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link> |
      <router-link to="/groups">Events Near Me</router-link>
    </div>

    <img alt="Vue logo" src="./assets/logo.png">
  </div>
</template>

<script>
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

#nav {
  padding: 30px;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
}

#nav a.router-link-exact-active {
  color: #42b983;
}

</style>

This issue highlights the challenges of troubleshooting common setup problems when working with Vue router and implementing it within a Vanilla Vue CLI environment.

Answer №1

Simply pass the router object to the initialization of a new Vue instance.

const router = new VueRouter({
  routes: [
    { path: '/', component: () => import('./views/Home.vue') },
    // { path: '/login', component: Login },
    // { path: '/about', component: About }
  ]
})

new Vue({
    router,
    render: h => h(App),
}).$mount('#app')

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 it possible to retrieve the var name from an interpolated expression using template literals?

Suppose I have a variable like this: myVar = `Some text with ${eggs} and ${noodles} or ${pies}`; Is there a method to obtain myVar as an unprocessed string prior to variable substitution, essentially "Some text with ${eggs} and ${noodles} or ${pies}"? M ...

Tips for delaying code execution until environment variables are fully loaded in Node.js/TypeScript/JavaScript

Purpose: ensure slack credentials are loaded into env vars prior to server launch. Here's my env.ts: import dotenv from "dotenv"; import path from "path"; import { loadAwsSecretToEnv } from "./awsSecretLoader"; const st ...

Pass multiple variables from a Laravel controller to a Vue component

Let's consider a Vue component as shown below: export default { created() { axios.get("a url").then(res => { console.log(res.data); }); } }; Next, Axios sends a request to the following function in a Laravel co ...

What is the best way to capture the input value upon pressing the "Enter" key?

My first question here is about implementing the addtodo(todo) code. After trying it out successfully, I wanted to make it work when typing and pressing enter. However, despite attempting some other methods, I couldn't get it to work. I didn't re ...

Functionality of Ajax: Data fields containing numerous values

I'm confused about the data fields in the ajax function. Typically, the syntax for an ajax function looks something like this: $.ajax({ url: "/aaa/bbb/ccc", method: "SomeMethod", data: someData, success: function (res ...

Creating a vendor bundle in create-react-appReady to optimize your create-react

When using the create-react-app tool, how can you specifically create a separate vendor bundle? While code splitting can be achieved easily with the react-code-splitting package, I have not been able to find clear instructions on generating vendor bundles ...

Illuminate a group of items when hovering over them

While attempting to change the color of 1 to 5 elements on mouse hover based on the data-rating tag, I am encountering a few issues. The data is retrieved correctly, but there are some problems: The function is being called 5 times each time there is a ho ...

Ways to restrict express API requests based on pricing tiers

I am currently developing a public API that includes a pricing plan for client accounts. I need to set limits on API requests based on each account's plan. My stack includes NodeJS and ExpressJS. Unfortunately, there seems to be an issue with the cod ...

What is preventing the use of data post submission in React JS?

After posting data to the navigation, I retrieve it and then use the following code: .then(data => console.log(data.PathPDF)) .then(data => window.open(data.PathPDF, '_blank', 'noopener,noreferrer')); While I can successfully ret ...

What methods are available for utilizing "open as" with commands in the shell?

After setting up a folder called my-express-server, I am eager to launch it directly from the shell using Visual Studio Code. To achieve this, I came across the command start [name of the file] -a [the specific path of the program I intend to use to open ...

The attempted creation of the ui.tree module failed with the error: [$injector:nomod] Unable to find module 'moduleName'! This could be due to a misspelling of the module name or

In my AngularJS project, I am utilizing a third-party library called angular-ui-tree to display data. To integrate this library, I need to include ui.tree as a dependency in my main app module. Following the instructions provided in the link, I have succes ...

Press a button to navigate through a series of web pages

Currently, I am working on a website focused on profiles. The challenge I am facing is implementing fixed side buttons (SKIP and ADD) that will cycle through the list of profiles. I understand that I may need to create an array to store all the profiles, ...

Having trouble getting the Node.JS Express Server to function properly on Enide?

My webserver is built using Node.JS with express and has multiple route commands. While everything works fine when running Node from the command line, I encounter an issue when running it within the Enide environment. Each request triggers an error messa ...

After activating the rewrite feature on Tomcat valve, JavaScript is loading twice

I've implemented the Tomcat rewrite valve in my single-page application to direct all requests, except for static resources, to my index.html file. Here is what my rewrite.config looks like: RewriteCond %{REQUEST_URI} (?!.*\.(?:jpg|png|css|js|js ...

"Triggering an AJAX POST request with a click event, unexpectedly receiving a GET response for

Hello there! I am currently attempting to send an AJAX POST request when a button is clicked. Below is the form and button that I am referring to: <form class="form-horizontal" > <fieldset> <!-- Form Name --> <legend> ...

Insert JavaScript code into the head of the document (including the complete script code)

I am looking to insert a script in the head section of a website so that the target HTML appears as follows: <head><script type="text/javascript">*some code...*</script></head>. This particular script works perfectly: var head = d ...

"Trouble arises when dealing with nested object arrays in Formik and handling changes in a child

I am struggling with passing a value to handleChange in formik validation. To address this issue, I created a component whose quantity is dynamically added based on the number of numChild. My goal is to allow users to click an icon and add any number of sk ...

"Learn the process of incorporating a trendline into a line chart using Highcharts by manipulating the

I am facing difficulties in creating a trend line for a line chart. I have tried some old solutions but they did not work for me. Below is my current code: { "key": "003", "title": "Detections", "ty ...

The browser unexpectedly cancelled the ajax call triggered by the beforeUnload event

Seeking advice on a web application that needs to save user input data through an ajax call when they leave the site. Currently using "Fetch" in a beforeunload event listener, but encountering issues with browsers cancelling the ajax call (specifically th ...

Utilize the push method to form a new array

var teamMembers = [ ['John D. Adams', '1959-1967', 'Ohio'], ['Dawson Mathis', '1971-1981', 'Georgia'], ]; To generate this dynamically, I am implementing the code below: var data = ne ...