Vue SPA: Datatables error when Page is reloaded
Vue SPA: Datatables error when Page is reloaded
Hi All,
I am using Vue CLI and building SPA application. I am using following frameworks.
- JQuery (As npm package)
- Bootstrap 4 (As CDN)
- Datatables (As CDN)
I am using datatable as shown: https://datatables.net/examples/styling/bootstrap4.html
This datatable is used on nested route within child component, which is working fine.
Please refer: Datatables_Before.png
But when the page is reloaded or refreshed, following is not working
- Show entries
- Search
- Sorting on each column
- Pagination
Error in console
vue.esm.js?65d7:479 [Vue warn]: Error in mounted hook: “TypeError: $(…).DataTable is not a function”
Please refer: Datatables_After.png
Answers
Possibly jQuery is being loaded multiple times? If its not that, we'd need a link to the page showing the issue to be able to understand what is going on.
Thanks,
Allan
jQuery is not loaded multiple times. I am using
VueJs
asSPA
along with DataTables.Steps to reproduce the issue.
npm start
- started my vue js appctrl + r
orF5
, DataTables is not working and I see this error in chrome consoleTypeError: $(…).DataTable is not a function
.On the same page, I have opened Chorm console and executed this command
$('#example').DataTable()
my DataTables are working again.According to my analysis, when the user refreshes the page, the command in
mounted()
is executed first before downloading the Datatables CDN. I am using JQuery as npm package so no issue with it.Source Code:
main.js
Source Code:
index.html
Source code:
component.vue
@allan
Please let me know if you need any further information.Please provide the fix, I have been blocked by this issue.
Happy to offer some help - but I would need a link to a page that shows the issue so I can offer that help.
It sounds very much like jQuery is being loaded again, which would remove any plug-ins. But without a test case and can't confirm that, or offer any alternatives since I can't debug it.
Allan