What could be causing Vue Router to only load after I click a link and then refresh the entire webpage?

I have a file called components.js filled with Vue.component instances.

Then I have a main.js where I implement a router using these components to create different pages.

In index.html, I have links to the router pages.

The issue is that on the initial page load, the template is not loading properly. When I click on the Home link, nothing happens. However, if I click on About or Project, the page does a full reload (with an iframe and prompt showing again). After clicking on About or Project, everything works as expected.

I even tried manually opening example.com/#/ in a new tab, but it's the same issue.

The code in my main.js file looks like this:

const About = { template: '<div>About</div>' }                        
const Home = { template: `                                            
<web-header></web-header>                                             
` }
const Project = { template: '<div>Project</div>' }

                        
const routes = [                                                        
{ path: '/', component: Home },                                       
{ path: '/about', component: About },                                 
{ path: '/projects', component: Project }
]                                                                                                                  

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

Answer №1

After some investigation, I have uncovered the root cause of the issue. It seems that the components were not being loaded before they were utilized.

To resolve this problem: Make sure to include the component files in your code BEFORE using them.

Feedback: I usually include all my scripts before the closing body tag.

In this particular scenario, it is essential to include the global instances of the components before the closing head tag (This is necessary as the HTML kebab syntax might not function properly without loading the component instances beforehand).

Everything is now functioning smoothly. Feel free to check out the commit to view the changes I implemented. (Just remember to rearrange the tags appropriately)

https://github.com/StringManolo/StringManolo.github.io/commit/fcc331dc5136fb8a0da5a4c979ab67c8b85365e7

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

Swiper V7 React is experiencing issues with numerous parameters not functioning properly

Here is the code snippet I have been working on: I am currently exploring the Swiper library documentation to understand its functionalities better. import React from "react"; // Direct React component imports import { Swiper, SwiperSlide } fro ...

Is the integer value included in the linear progression?

I have a unique setup where each time the user clicks a 'Done' button, 20 is added to a base number, x (..-40,-20,0,20,40,60..). This updated value of x is then saved in a database and displayed in real-time using Ajax. However, I am facing a ch ...

Obtaining data from JSON arrays

My current challenge involves extracting data from the following link: and storing it in my database. However, I am encountering difficulties with manipulating the array in order to retrieve the last unix datetime. Despite multiple attempts to extract th ...

Tips for showing/hiding textboxes based on select options:

I am currently working on a project that allows users to enter their personal information. I need help figuring out how to show or hide textboxes based on the selection made in a dropdown menu. The dropdown menu in question is for marital status. The opt ...

Is it necessary to create a Node endpoint for each item in the array?

My current tech stack includes ExpressJs, NodeJs, and AngularJs. Imagine I have an array containing various objects representing grocery store accounts and the amounts owed to them by the bank. [{ account: 1, amount: 2.33 }, { account: 2, amount: 5.99 ...

problem when trying to establish the minimum height for a div element prior to the website being fully loaded, considering the

Having trouble with a CSS issue that seems simple but I can't find a solution for. I have a main div with a min-height set to a certain value, specified in %. Since there is no outer div, the min-height won't display if it's given in px. I ...

Storing Data Property Value from an Item in Rendered List Using Vue: A Quick Guide

I'm currently working on implementing a follow button for list items in Vue. My approach involves extracting the value of a specific property from a list item and storing it in the data object. Then, I plan to utilize this value within a method to app ...

Nextjs unexpectedly displays blank page upon fetching data from Firebase Firestore without any error messages

I am currently facing an issue when trying to retrieve page details from Firebase Firestore using getStaticPaths and getStaticProps in my Next.js project. Despite following the code structure correctly, I am encountering a scenario where the page appears e ...

Having trouble retrieving the state value of an array in React?

Having issues with ...this.state.users in React My React Code: handleChange(i, e) { const { name, value } = e.target; let users = [...this.state.users]; users[i] = { ...users[i], [name]: value }; this.setState({ users }); } Snippet from ...

Create a function that can dynamically filter an array of objects and extract specific properties into a new array

Here is the input that I have: [ { "metadata": { "id": 1071, "name": "First" }, "languages": [ { "name": "Alpha", "details": [ { "city" ...

execute javascript code found in the html document

Currently, I have a bash script that utilizes curl to download a page. Then, it uses grep and sed to extract javascript within the html block to a file. Following this, I use node to evaluate and leverage the downloaded javascript. Here is an example: cur ...

When an element is transferred to a different <div>, it does not automatically inherit its new CSS styles

There's an element with existing behavior that needs to be moved to meet new requirements without losing its original styles. The challenge is applying new styles to the element after it's been moved. For example: <div id="existingStructure" ...

Assign the Firebase token to the JavaScript cookie value

Can a cookie store a token value? In my setup with js-cookie, Firebase auth/firestore, and Next.js, I am setting my cookie within the handleUser function like this: const handleUser = async (rawUser) => { if (rawUser) { const user = await fo ...

Vue bug: offsetTop consistently outputs 0

When attempting to fix an element when the user scrolls past it, I encountered an issue. Despite the element (in this case a navbar) being approximately 300px in height, it consistently returns a value of 0. Within my mounted method where the calculation ...

Implement a jQuery loading animation triggered by scrolling down the page

Can anyone offer guidance on how to trigger an animation as you scroll down a webpage? I've come across this feature while browsing through this website: I would love to include code examples, but I'm unsure of where to start with implementing t ...

Guide on building a REST API with Node.js Express.js and OracleDB to perform database updates

Looking for assistance with creating an API in Node.js to fetch and update data on the frontend. I am able to retrieve data using the provided code, but facing challenges updating it. React.js is being used for the frontend. var express = require("expr ...

Having trouble with the JsonLoader copied from Threejs.org?

I've been trying to make use of a JSONLoader from threejs.org, but I'm facing some issues. Three.js seems to be functioning properly because I can easily create a cube. However, when I attempt to load a js file through the JSONLoader, nothing hap ...

Controlling the Vuetify Navigation drawer from a separate component

Within my App.vue component, I have a Navigation Drawer menu containing main items and some subitems. These subitems are meant to be displayed only after a specific event, such as a button click on the child component's page (when the variable totalRe ...

I am puzzled by the presence of the 'x' value in the JSON object when creating a column chart using CanvasJS. Its origin remains unknown to me

I'm exploring the use of canvasjs and I have a test code snippet that I'm working with: 9 $con = pg_connect("host=localhost port=5432 dbname=testdb user=postgres") or die ("Could not connect to server\n"); 10 $query ="SELECT id as label, ...

What's the best way to save data from an HTML input field into a JavaScript array?

Can someone help me with storing a value from an HTML input field into a JavaScript array after a button click, and then displaying that array data on the screen? Here is the HTML code I am currently working with: <!DOCTYPE HTML> <html> < ...