Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

How to import a JS file in vue ?

Hello ,

I created a function in a file getAllUrlParams.js :

function getAllUrlParams(url) {

    // get query string from url (optional) or window
    var queryString = url ? url.split('?')[1] : window.location.search.slice(1);

    // we'll store the parameters here
    var obj = {};

    // if query string exists
    if (queryString) {
...

I woulf like to use that function in my vue file :

<script>
    import taskDetails from "./taskDetails";
    import companyDetails from "../companies/companyDetails";
    import addTask from "./addTask";
    import editTask from "./editTask";

    export default {
        name: "tasks",
        components: {

1- how to import it ?

2- what is the best place to put that JS file ?

Thanks

0 likes
2 replies
dorel_besliu's avatar

Hello,

You can create a folder and name it "helpers" or "utils", put your folder in resources/js/

Create your function and export:

    const getAllUrlParams = () => { //your function body }

    export default getAllUrlParams;

Import your function in your component or view:

<script>
    import taskDetails from "./taskDetails";
    import companyDetails from "../companies/companyDetails";
    import addTask from "./addTask";
    import editTask from "./editTask";
    import getAllUrlParams from "./helpers/getAllUrlParams";
samgoodger's avatar

In your getAllUrlParams.js file, put export default before the function keyword.

Like this:

export default function getAllUrlParams(url) {
... // code here
}

Then you can import it easily in your Vue script:

<script>
    import taskDetails from "./taskDetails";
    import companyDetails from "../companies/companyDetails";
    import addTask from "./addTask";
    import editTask from "./editTask";
    import getAllUrlParams from "./getAllUrlParams";
...

As for where is best to store the file, it's really up to you. Ideally you would keep all common functions in a separate folder to keep things organised.

Please or to participate in this conversation.