Why isn't my router link clickable? What mistake did I make?

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Products from '../views/Products.vue'
const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/products',
        name: 'Products',
        component: Products
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

export default router

This code snippet represents the index.js file for my router. Below is a snippet from my app.vue file.

   <template>
 <header class="top-bar spread">
      <nav class="top-bar-nav">
        <router-link to="/" class="top-bar-link">
          <i class="icofont-spoon-and-fork"></i>
          <span>Home</span>
        </router-link>
        <router-link to="/products" class="top-bar-link">
          <span>Products</span>
        </router-link>
        <router-link to="/past-orders" class="top-bar-link">
          <span>Past Orders</span>
        </router-link>
      </nav>
      <router-link @click="toggleSidebar" href="#" class="top-bar-cart-link">
        <i class="icofont-cart-alt icofont-1x"></i>
        <span>Cart ({{totalQuantity}})</span>
      </router-link>
    </header>
    <router-view/>
</template>

<script>

export default {
  name: 'App',
}
</script>

Currently, I'm following a tutorial and experiencing issues with my links not working. Despite setting up paths in the index file and installing vue-router correctly, my landing page displays plain text only.

When I change the order of 'use' before mount as shown below, I do not encounter any errors:

const app = createApp(App); app.use(router); app.mount('#app');

Error:

Uncaught TypeError: Cannot destructure property 'options' of '(0 , vue__WEBPACK_IMPORTED_MODULE_1__.inject)(...)' as it is undefined.

Answer №1

Thank you for sharing the repository link, it was really helpful in identifying the issues:

  1. Vue Router is missing from your dependencies.

npm i vue-router

  1. There were some HTML errors in your App.vue file. Here's the corrected version:
<template>
  <header class="top-bar spread">
    <nav class="top-bar-nav">
      <router-link to="/" class="top-bar-link">
        <i class="icofont-spoon-and-fork"></i>
        <span>Home</span>
      </router-link>
      <router-link to="/products" class="top-bar-link">
        <span>Products</span>
      </router-link>
      <router-link to="past-orders" class="top-bar-link">
        <span>Past Orders</span>
      </router-link>
      <button @click="toggleSidebar" class="top-bar-cart-link">
        <i class="icofont-cart-alt icofont-1x"></i>
        <span>Cart ({{ totalQuantity }})</span>
      </button>
    </nav>
  </header>
  <router-view />
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      totalQuantity: 10,
    };
  },
  methods: {
    toggleSidebar() {
      console.log("toggling sidebar");
    },
  },
};
</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;
}
</style>

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

Jquery pop-up information bubble

I'm currently using: jQuery Bubble Popup v.2.3.1 http://maxvergelli.wordpress.com/jquery-bubble-popup/ The following code should be included within the document ready section: $('.bubble').CreateBubblePopup({ align: 'cent ...

Sending information to a server using JavaScript

Greetings everyone, this is my first time posting here and I would appreciate detailed guidance in your comments. Just a little background about myself: I am a second-year college student with one year of Python training. I only recently started learning ...

jQuery conceal input field after it has been displayed

I've created an HTML form with two different sections for search purposes. The first section displays basic fields, and by clicking on "Advanced Search" (in my case, "Расширенный поиск"), additional fields are revealed. Everything work ...

Tips for managing large amounts of data retrieval from an API

As a beginner, I am attempting to retrieve data from an API and display it using the v-for directive. However, this process is causing my app to lag. It freezes when new data is fetched or when I search within the list. The following code snippet shows whe ...

Tips for adding a new column to a website

My goal is to inject some custom HTML and CSS into YouTube in order to create a column on the right side that shifts all content towards the left. Essentially, I am trying to replicate the functionality of the Inspect Tool in Chrome. I am working on a Chr ...

Is it beneficial to use TypeScript for writing unit tests?

We are in the process of transitioning from JavaScript to TypeScript within my team. One question that has come up is whether we should also migrate our unit tests from JavaScript to TypeScript. Personally, I am not convinced of the significant benefits o ...

Developing a state object encompassing all the columns of a data table for sorting purposes in React JS

Trying to create a state object to maintain field names and sorting types (ascending/descending). Implementing server-side sorting by passing the column name and sort type (asc or desc). I have a component for a data table with click handlers to determin ...

A tip for transferring the value of a binding variable to a method within the template when the button is clicked

I am currently exploring the concept of binding between parent and child components using @Input, @Output, and EventEmitter decorators. This is demonstrated in the HTML snippet below: <h1 appItemDetails [item]="currentItem">{{currentItem}}& ...

Ways to display a different landing page when navigating to the homepage of a website

In the Next application, I have set up a dynamic route at the root of my pages folder as src/pages/[page].js While this works smoothly for pages with slugs like example.com/my-page, it poses a challenge when trying to access a designated slug named homepa ...

Align the Bootstrap Nav Bar in the Center

<!-- Unique Header --> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-togg ...

ERROR occurred in the renderer.js file due to an unexpected punctuation token «(»

Version: webpack 2.3.3 Hello, I am currently working on developing my first Electron app using Vuejs. The process has been smooth so far until I encountered an issue during the packaging phase. Running npm run dev works perfectly fine, but when I execut ...

What is the best way to dismiss a custom popover when clicking outside of it?

I'm currently developing a Cordova application. I have opted to utilize Vue.js and jQuery for bindings and scripts, while also taking on the responsibility of designing the user interface myself. While I've managed to implement page transitions a ...

Monitoring the setup file in a node.js environment

Currently, the node-config npm package is being utilized to manage the configuration of node.js. An obstacle I am facing is the need to monitor the file being used by node-config. Unfortunately, it appears that the capability to watch files has been remov ...

having trouble accessing a JavaScript array in AngularJS

Hey there, I'm currently working on a web application using AngularJS and I'm facing an issue with querying arrays. Check out the code snippet below: var angulararray = []; bindleaselistings.bindleaselistingsmethod().then(function(response) { ...

Connect an angular directive template to an event

In my Angular directive, the template structure is as follows: <Button id="testBtn">Test me<button> Within the directive controller, I have defined API functions for the directive. Now, I need to send an event to the parent module when the bu ...

Send the user to a 404 error page in Vue.js without changing the URL

Currently in my Vue.js project, I am showing the 404 page when a route parameter is invalid by using this code: this.$router.replace({ path: '/404' }); Is there a way to achieve this without changing the URL? I want users to be able to copy the ...

The Bootstrap modal needs to display the specific table row that was retrieved from a JSON file in Angular

My table is populated using ng-repeat with data sourced from JSON. The table includes columns for name, phone, and time. When I click on a name, a bootstrap modal appears. However, I need the selected name to be displayed within the bootstrap modal itsel ...

Optimizing React App Development: Configuring dev-server for optimal port performance

Looking for a way to set your own script to the BROWSER environment variable? Check out the guide at https://facebook.github.io/create-react-app/docs/advanced-configuration. In my script, I want to direct users to another page with a query parameter that ...

Preventing child element clicks in Angular 5: A helpful guide

I have checked numerous references, but unfortunately haven't received any responses. That's why I have turned to this platform in hopes of improving my code with your assistance. I need to add an element with text on click. Currently, it works ...

Control the prompt with the Puppeteer typing function

Hello, I am currently attempting to log into a system that looks like the following: The input fields are labeled as username and password, with buttons labeled as login and cancel. I am trying to input data into these fields and click on the login ...