Importing JavaScript files to work with Vue-Router: A Step-by-Step Guide

Seeking guidance on importing JavaScript files correctly throughout my Vue project, including within the components used to implement changes with Vue-Router. Currently, encountering an issue where I must reload the component in order for the JavaScript to load properly; when navigating routes and changing components, the JavaScript functions only work after a manual refresh. It seems that I may not be including them correctly, as the Router Link alone is insufficient, leading to the Router View not loading the JavaScript for each component based on the route.

Here's a snippet from my index.html:

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>vue-sfi</title>
    <!-- Font Awesome -->
    <link rel="stylesheet" href="./static/Plugins/plugins/fontawesome-free/css/all.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <!-- Tempusdominus Bootstrap 4 -->
    <link rel="stylesheet" href="./static/Plugins/plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="./static/Plugins/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
    <!-- JQVMap -->
    <link rel="stylesheet" href="./static/Plugins/plugins/jqvmap/jqvmap.min.css">
    <link rel="stylesheet" href="./static/Public/css/material.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="./static/Plugins/dist/css/adminlte.min.css">
    <!-- overlayScrollbars -->
    <link rel="stylesheet" href="./static/Plugins/plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="./static/Plugins/plugins/daterangepicker/daterangepicker.css">
    <!-- summernote -->
    <link rel="stylesheet" href="./static/Plugins/plugins/summernote/summernote-bs4.css">
    <link rel="stylesheet" href="./static/Public/css/styleSGB.css">
    <link rel="stylesheet" href="./static/Public/css/aside.css">
    
    // More links...
    
  </head>
  <body>
    <div id="app"></div>

<!-- JavaScript imports -->

// More script imports...

  </body>
</html>

If needed, here's the file structure for reference:

/
    |--> index.html
    |-->  /src
    |     |--> /components    <----Vue Router components
    |     |    |--> /interfaces
    |     |         |--> /afiliacion
    |     |              |--> afiliacion.vue
    |     |    |--> /layouts
    |     |         |--> /modules
    |     |              |--> aside.vue
    |     |              |--> footer.vue
    |     |              |--> header.vue
    |     |--> /router
    |     |    |--> index.js <----routes
    |     |--> App.vue
    |     |--> main.js
    |--> /static
         |--> /plugins <---- CSS and JavaScript files
         |--> /public  <---- CSS and JavaScript files.

Additionally, here's how the routes are set up:

// Route file

import Afiliacion from '@/components/Interfaces/Afiliacion/Afiliacion.vue'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login,
      meta: {
        requiresVisitor: true
      }
    },
    {
      path: '/logout',
      name: 'Logout',
      component: Logout
    },
    {
      path: '/Home',
      name: 'Layout',
      component: Layout,
      meta: {
        requiresAuth: true
      },
      children:[
         {
      path: 'Interfaces/Afiliacion',
      redirect: '/Home/Afiliacion',
      name: 'Afiliaciones',
      component:{
        render (c){ return c('router-view') }
      },
      children: [
        {
          path: '/AfiliacionFondo',
          name: 'Afiliacion',
          component: Afiliacion,
          meta: {
            requiresAuth: true
          }
        }]

Considering placing the CSS and JavaScript files in the src/assets folder as per common practice. Any assistance in resolving this issue would be greatly appreciated.

Answer №1

Simple method for incorporating jQuery into a vue project.

Install necessary packages

npm install vue-jquery --save

Add to main.js file

import vue-jquery from 'vue-jquery'

Vue.use(vue-jquery)

If you want to use external libraries in vue, npm installation is required. Then import it as shown above with jQuery.

Explore AdminLTE Vue

Check out the Vue AdminLTE by simply executing npm i.

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

Selecting a specific section from a JSON data reception

Upon executing a POST request to , the resulting response typically looks like this: { "success": true, "data": { "url": "http://i.imgflip.com/1nciey.jpg", "page_url": "https://imgflip.com/i/1nciey" } } Is there a way for me to output the s ...

Search for all documents in MongoDB and update all of them except for one

After performing a database lookup, I am receiving an array of objects as a result. [ { name: "test", services: { credentials: "123456" } }, { name: "test1", services: { credentials: "1 ...

Utilize $emit without the need for a click event

Vue is new to me and there are still some aspects that I haven't completely grasped. I noticed that in the first child component, there is a click event used to $emit information to the next child component. However, in the second child component, is ...

Playing embedded YouTube videos automatically in Safari 11 without user interaction

I’m encountering an issue with a simple modal dialog: When a user clicks on a button, the modal overlay appears. An embedded YouTube <iframe> is then added. Everything works smoothly in most browsers, except for Safari 11.1. Safari’s new auto ...

Tips for enhancing the speed of drawing circles in mouse movement using JS and CANVAS

My current script allows me to draw a circle during mousemove, but it's not as fast as I'd like. Before drawing the circle, I gather the color pixel during mousemove in order to draw a circle on the canvas with the picked color. I came across ...

Deploying a Vue application to Internet Information Services (IIS) within the Default Website

I managed to successfully run a Vue app in IIS at the root level with port 8181, but now I need to set it up under the Default Web Site in IIS using port 80. To test this, I created a new website with the VS19 template (should have used Vue scaffolding, w ...

Error occurs when incorporating OrbitalControl into Three.js leads to TypeError

Attempting to incorporate OrbitalControls into a Three.js Script to allow users to view two spheres in an empty space. However, upon running the code, the following error is encountered: three.js:5353 Uncaught TypeError: Cannot read property 'center& ...

Steps to deactivate two choices in a multi-select dropdown menu and visually dim those options

Hey there, I recently worked with Angular8 and Bootstrap 4. I utilized a Bootstrap multi-select dropdown, where I encountered an issue: specifically, I'm trying to disable and gray out the options for PL Marketing and CL Marketing but have been unsucc ...

Using state in a ReactJS and Rails Ajax request

I am currently working on a ProductItem component that triggers an AJAX post call to create a basket item when the user clicks 'Add to Basket'. Despite setting state correctly in the Product Item component, I am facing an issue where I am unable ...

Modifying the page's left attribute dynamically with jQuery based on window resize

I am currently facing a small issue with my code. My goal is to update the 'left' CSS property of certain elements based on the difference between their current left value and the page resize amount. This way, when the background moves with a pag ...

Trouble loading CSS file in Vue library from npm package

When using vue-cli to build a library (npm package) that functions for both SSR and client-side, everything seems to be functioning correctly except for one issue; the CSS only loads if the component is present on the page being refreshed. However, when ac ...

Enabling the acceptance of blank values within an HTML5 date input field

When using an HTML5 date input for a field that corresponds to a nullable datetime column in the database, how can one avoid setting an empty value in the input field? In my AngularJS application, the ng-model is connected to a scope variable with an init ...

Utilizing Vue Router for service integration

I have configured my vue router for external components and services, setting it up as follows: /* Router.ts */ import { myRoute } from './views/myRoute.vue' const router = { routes: [ { name: 'my-router', ...

Can you provide guidance on decompressing SVGZ files using JavaScript?

I'm currently working on implementing a high-resolution world map using SVGZ instead of the standard SVG format, aiming to provide my users with a more intricate and detailed visualization. Although I have experimented with utilizing js-deflate to de ...

What is the best way to develop shared singleton components that work seamlessly across various platforms?

How about developing a React component called LoadingMask that can toggle the display of a loading mask based on the current state? The purpose would be to show the mask before an ajax call and hide it once the data is received. To avoid showing multiple ...

Is it possible for JavaScript/React to observe the movement of elements on the webpage?

I'm searching for a solution to detect if an element moves on the screen. Currently, I have an absolute positioned div (acting as a download overlay) that appears when a document is clicked on my website. However, I want it to disappear whenever the d ...

Refreshing a div using ajax technology

I am attempting to use Ajax to update an h3 element with the id data. The Ajax call is making a get request to fetch data from an API, but for some reason, the HTML content is not getting updated. This is how the JSON data looks: {ticker: "TEST", Price: 7 ...

Creating a prompt within a while loop

Issue with the code is that it should only progress if the user inputs "rock", "paper" or "scissors". However, after re-entering any input the second time, it still moves on despite passing the condition in the while loop. For instance, entering "asdf" p ...

Permanently dismiss Bootstrap 4 alert using a cookie

Recently, I came across a bootstrap 4 alert that I found quite useful. Here is the code snippet for it: <div class="alert alert-warning alert-dismissible fade show" role="alert"> <button type="button" class="clo ...

Authenticating child in Azure JWT token using the kid value hardcoded

As I obtained an Azure token, I decided to verify it by checking the kid in the header. I decoded the token on jwt.io and hardcoded the kid into my code for future tokens. However, after a few days, the public keys were updated, rendering the previous kid ...