I’m having trouble with loading JavaScript files properly in my Vue application when using Vue Router. The issue is that my JS functions only work after I manually refresh the page with F5. When I navigate between routes using router-link, the JavaScript doesn’t load correctly for each component.
My setup:
I have a main app.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My Vue App</title>
<!-- CSS Libraries -->
<link rel="stylesheet" href="./assets/libs/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="./assets/libs/datatables/datatables.min.css">
<link rel="stylesheet" href="./assets/custom/main.css">
</head>
<body>
<div id="root"></div>
<!-- JavaScript Libraries -->
<script src="./assets/libs/jquery/jquery.min.js"></script>
<script src="./assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="./assets/libs/datatables/datatables.min.js"></script>
<script src="./assets/custom/app.js"></script>
</body>
</html>
Project structure:
project/
├── app.html
├── /src
│ ├── /views
│ │ ├── /pages
│ │ │ └── UserProfile.vue
│ │ └── /shared
│ │ ├── Sidebar.vue
│ │ └── TopBar.vue
│ ├── /routing
│ │ └── routes.js
│ ├── Main.vue
│ └── index.js
└── /assets
├── /libs
└── /custom
Router configuration:
import UserProfile from '@/views/pages/UserProfile.vue'
Vue.use(VueRouter)
const appRouter = new VueRouter({
mode: 'history',
routes: [
{
path: '/dashboard',
name: 'MainLayout',
component: MainLayout,
children: [
{
path: 'profile',
name: 'UserProfile',
component: UserProfile
}
]
}
]
})
Should I move my JavaScript files to the /src/assets directory instead? What’s the correct approach to ensure scripts load properly when navigating between routes?