Component not loaded by vue.js router-link

I am a beginner with vue.js and I have created a Landing component that is connected to the Login component. My goal is to make it so that when the user clicks on Login, the login page displays.

<template>
        <div>
            <div class="landing">
                <router-link to="/login">Login</router-link>
                </div>
            </div>
        </div>
    </template>

    <script>

    export default {
        name: 'Landing',
        data: function () {
            return {
            }
        },
        methods:  {

        }
    }
    </script>

The main.js:

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import Materials from "vue-materials"

import Routes from './routes'

const router = new VueRouter({
    routes: Routes,
    mode: 'history'
});


Vue.use(Materials)
Vue.use(VueRouter);

Vue.config.productionTip = false

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

App.Vue :

<template>
  <div id="app">
        <head>
          <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
          <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.css" rel="stylesheet">
        </head>

        <NavbarComp/>        
        <Landing/>
        <FooterComp/>
  </div>
</template>

<script>
import NavbarComp from './components/Navbar.vue';
import FooterComp from './components/Footer.vue';
import Landing from './components/Landing.vue';
import Login from './components/Login.vue';
import Register from './components/Register.vue';

export default {
  name: 'app',
  components: {
    NavbarComp,
    Landing,
    FooterComp,
    Login,
    Register
  }
}
</script>

routes.js:

import Login from './components/Login.vue'; 
import Register from './components/Register.vue'; 
import Landing from './components/Landing.vue'; 

export default [
  {path: '/login', component: Login, name: 'Login'},
  {path: '/register', component: Register, name: 'Register'},
  {path: '/', component: Landing, name: 'landing'},
]

And finally, Login.vue:

<template>  
  <div>
     <h2>Login</h2>    
   </div>

</template>

<script>

import axios from 'axios';

export default {
  name: 'Login',
  data: function () {
      return {
        ok: true,
        showErrorRegister: false,
        showErrorLogin: false,
        username: "",
        password: "",
        email: "",
        error: "",            
      }   
  },

When I click on Login link, the URL changes but the component does not display, and there are no errors in the console. I'm not sure what to do next.

What steps can I take to resolve this issue?

Answer №1

Expanding on Cory's comment - the missing piece here seems to be <router-view>.

As it stands, your app seems to be functioning because in App.Vue you are displaying the <Landing/> component. To improve this setup, consider replacing <Landing/> with <router-view/>. This way, when the route path is "/", the <router-view> will display the <Landing/> component as before. And when the route path changes to "/Login", the Router will correctly show the <Login/> component in that space.

The Router currently knows where to find the Login component, but it needs a designated spot to display it.

For further information, check out: https://router.vuejs.org/guide/#html

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

Guide on assigning a value to a material ui select box

Currently, I am utilizing the material UI Select component for the year field in my project. What I aim to achieve is setting a default year based on the value present in the state. To populate the years, I am using an array of years. Here is the method r ...

The npm error message states: "Unable to locate the 'readable-stream' module."

Recently, I installed node js on my Windows 10 machine with version v4.4.2. However, whenever I attempt to run npm install or check npm version, it throws the following error message. Any assistance on this matter would be highly appreciated. Error: Canno ...

The initial UI did not match the server-rendered content, resulting in a Next.JS Hydration failure

I'm facing an issue with hydration in Next.JS 14, where there is a discrepancy between the server-side rendered UI and the client-side rendering. I have a suspicion that this problem may stem from using new Date(), which could be producing different v ...

Appending a property to a JSON object using JavaScript

What is the method for adding a new field to a JSON object in JavaScript? I have seen examples using arrays, but I specifically need it with a JSON object. Essentially, how can I transform this: { "hello":"this is cool" } into this ...

Upon introducing the CSS loader into the webpack configuration, TinyMCE unexpectedly ceases to function

My application development journey began with cloning code from https://github.com/DMPRoadmap/roadmap. This project is based on webpack and npm. To integrate select2, I executed npm install select 2 in the lib/assets directory. I aimed to incorporate a ...

Pressing the Enter key on a textarea does not trigger data fetching

After clicking the button, all values are displaying correctly. However, when I try to use the ENTER key on the textarea to send the data, the data is not showing up. I have attempted the code below but it only shows an empty div. You can find the jsfiddle ...

javascript for each and every textarea

I am looking to apply my JavaScript code to all the Textarea elements on my page. $_PAGE->addJSOnLoad(" $('.textarea').each(function() { $(this).keyup(function() { var characterCount = $(this).val().length; var mes ...

Populating a Dropdown Menu with JSON Data using JavaScript

I am facing an issue with populating my dropdown menu using data from my JavaScript file. Whenever I click the button, three objects are added to the dropdown, but I only want them to show individually upon clicking. Additionally, I am getting [object obje ...

How to incorporate a delay in ng-repeat using AngularJS

Currently, I am facing an issue with my ng-repeat block. In this block, I am generating elements based on data received from an ajax request, which sometimes causes a delay due to latency. Within the same block, I have implemented a filter to remove unwant ...

Unable to successfully register a ServiceWorker due to an unsupported MIME type ('text/html') in Vue.js

I have successfully implemented a service worker in my Vue.js project before with simple HTML projects, where it worked great. However, when I include the same file in my Vue.js code, it keeps throwing an error: The script has an unsupported MIME type ( ...

Using jQuery to select a specific checkbox from a group of checkboxes with identical IDs

There is an issue with multiple checkboxes having the same ID in an asp.net repeater control. Users can select either email or phone against each record in the repeater rows. In the example below, there are two rows. If you select the email icon in the fi ...

Can anyone provide guidance on how to concatenate an array of JavaScript functions from another JS file?

Within my array of functions, the structure is as follows: var myFuncs = { firstFun: function(string) { // perform a task}, secondFunc: function(string) { // perform a task}, thirdFunc: function(string) { // perform a task }} I am looking to extract and ...

Tips for performing intricate sorting within a jQuery tableSorter

I am facing an issue with my table data structure. Cell1 Cell2 Computers 10 Dell 5 IBM 3 Compaq 2 HDDs 12 Seagate 7 Samsung 3 Test 2 Monitors 18 Seagate 7 Samsung 9 D ...

expanding animation featured on Wikipedia

Have you ever noticed that when you visit a Wikipedia page on Chrome and use the ctrl+scrollup or ctrl+scrolldown command, the page resizes in a smooth animation? Do you wonder how this effect is achieved? (Interestingly, in Firefox, only the links in th ...

Exploring the concept of Variable Scope within a jQuery promise

Picture yourself executing the following code snippet within a JavaScript function named Fetch. function Fetch(context) { var request = $.ajax({...}); request.done(function(response) { // It appears that context is accessible here and in scope. ...

We are experiencing a peculiar issue with the camera functionality in ThreeJS

I have created a scene in ThreeJS featuring two cubes, a camera, a light, and a plane. The cubes rest on the plane while the camera gracefully circles around the green cube. Despite my efforts, I am encountering an unusual issue with the camera angles. As ...

How can the total price for a shopping cart be computed in React/Redux?

After extensive searches on Google and SO, I'm still coming up empty-handed when it comes to calculating the total price of items in a cart. Many tutorials stop at basic cart functionalities like "Add item to cart" or "increase/decrease quantity", but ...

What steps can be taken to empower users to make changes to pre-selected data within the vue-select component?

Usage of vue-select component: Currently, I am looking for a way to enable my users to edit data that has already been selected. After going through the documentation, I couldn't find a direct method to achieve this. If anyone has experience working ...

"Discrepancy found in results between Node-Fetch POST and Firefox POST requests

I have encountered a challenge while trying to replicate a successful log-in process on a website using node-fetch after being able to do so with Firefox. The login process consists of three stages: Visiting /login which returns a sessionToken from the we ...

What is the best way to divide a single object in an array into multiple separate objects?

In my dataset, each object within the array has a fixedValue property that contains category and total values which are fixed. However, other keys such as "Col 2", "Col 3", etc. can have random values with arbitrary names like "FERFVCEEF erfe". My goal is ...