3

Im trying to build single page app using Vue + inertia + laravel. I have an html template that include external js file. When integrating the template with vue, i notice that, the js only run only once after the page is refresh and not applied to the other page (when navigate using router-link)

i tried some method:

A. add the js script within vue template using mounted

mounted() {
        let appscript = document.createElement("script");
        appscript.setAttribute(
            "src",
            "/dist/js/app.js"
        );
        document.head.appendChild(appscript);
    }

B. Add the js script using meta info

metaInfo: {
    script: [
      {
        src: "/dist/js/app.js",
        async: true,
        defer: true,
      },
    ],
  },

here is my blade file

<!DOCTYPE html>
<html lang="en" class="light">

<head>
    <meta charset="utf-8">
    <link href="/dist/images/logo.svg" rel="shortcut icon">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>
        CollaborApps
    </title>
    <link rel="stylesheet" href="{{ asset('dist/css/app.css') }}" />
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <script type="application/javascript" src="{{ asset('/js/app.js') }}" defer></script>
    <script type="application/javascript" src="{{ asset('/dist/js/app.js') }}" defer></script> <- this is the external js
    @routes
</head>

<body class="main">
    @inertia
</body>

</html>

How to integrate javascript file with vue SPA?

Wailan Tirajoh
  • 481
  • 5
  • 17
  • Is the external script written by you? Is it some sort of service API, like Vimeo, Youtube, Facebook, Instagram? Are you using a bundler, like webpack? – Matheus Dal'Pizzol Sep 21 '21 at 17:20
  • @MatheusDal'Pizzol its external script from html template (midone-jquery-tailwindcss-html-admin-template). this external js include some plugins like summernote, tomselect, ect. the plugin didnt work with vue router. – Wailan Tirajoh Sep 21 '21 at 17:41
  • I absolutely understand what you want to accomplish here. I'm just not sure if I would even recommend going this route. I feel like using both Vue and jQuery on the same page is not only unnecessary, but it will take your time figuring out how to get both of them working together and also throw you into a lot of situations and bugs that can be hard to track. I would recommend taking the time to select the plugins you need and found Vue versions of them, and then importing them globally or locally in you Vue files via your bundler. Laravel Mix can take care of the codesplitting for you. – Matheus Dal'Pizzol Sep 21 '21 at 21:46
  • thanks for straightening this out mate, ill go for plugin that suit for Vue then! have a good day! – Wailan Tirajoh Sep 22 '21 at 03:25
  • Does this answer your question? [How to add external JS scripts to VueJS Components?](https://stackoverflow.com/questions/45047126/how-to-add-external-js-scripts-to-vuejs-components) – Matheus Dal'Pizzol Sep 22 '21 at 09:56
  • @MatheusDal'Pizzol I tried that also, the method work for append script tag to html. – Wailan Tirajoh Sep 22 '21 at 13:51
  • Ok... Just to be clear: THERE ARE some situations where you can't rely on your bundler and you'll need to reach for the "append script solution" or just put the script tag on your template. For instance: you need to render social media embeds with the Facebook, Twitter, Instagram SDKs, because these scripts can't be bundled. But, in your case, I would stick to what I said earlier. Good luck! – Matheus Dal'Pizzol Sep 22 '21 at 14:24
  • Did u find any solution? – Umer Usman Nov 18 '21 at 14:07
  • I replace all the broken js with some plugin that compatible with vue @UmerUsman – Wailan Tirajoh Nov 19 '21 at 04:49

0 Answers0