1

I have a javascript function in a separate js file toaster-message.js, the file is in wwwroot/js of ASP.net application, which calls the bootstrap toaster.

toaster-message.js.

showCompleteToast: (message) => {

    const toastElm = document.getElementById('#complete-toast');
    const toast = new bootstrap.Toast(toastElm)

    toast.show();
}

I want to call the showCompleteToast() from my vue instances. I am creating the vue instances with Direct script Include.

I don't want to add any Vue dependency to the function outside the Vue instances. So what is the proper way to call the function in the external js file that is outside the Vue instances?

@section scripts {

    <script>

        const app = new Vue({
            el: "#app",
            data() {
                return {

                }
            },
            methods: {
                showToast: function(){
                   //I want to call the show toast from here
                },
                submit: async function () {
                    try {

                        this.showToast();

                    } catch (error) {
                      
                        console.log(error)
                    }
                }
            }

        });
    </script>
}

When i try to import using:

import { showCompleteToast } from "~/lib/js/toater-message.js"

while using:

export default {
   showCompleteToast: (message) => {

       const toastElm = document.getElementById('#complete-toast');
       const toast = new bootstrap.Toast(toastElm)

       toast.show();
   },
   // ... other methods here
};

I get the error as:

“Uncaught SyntaxError: Cannot use import statement outside a module”

I tried to to import using:

<script type="module">
    import { showCompleteToast } from "../../wwwroot/js/toaster-message.js"
    alert(showCompleteToast)
</script>

This gave the error as:

GET https://localhost:44307/wwwroot/js/toaster-message.js net::ERR_ABORTED 404
9
  • 3
    "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 Commented Jun 14, 2021 at 17:35
  • 1
    I don't want to add any dependency to the external tools and plugins.. still, the attached post doesn't have any accepted answer. Commented Jun 14, 2021 at 17:44
  • 1
    @ aakash, no accepted answer simply because OP don't select one. Commented Jun 14, 2021 at 17:48
  • 1
    @ aakash. and many answer there doesn't need any dependency Commented Jun 14, 2021 at 17:51
  • 1
    none of them is working in the .net application Commented Jun 14, 2021 at 17:56

1 Answer 1

1

I'm not very familiar with php but typically you can import JavaScript files and then work with their contents. The only requirement is that the imported files need to have exporting defined.

// toaster-message.js

export default {
   showCompleteToast: (message) => {

       const toastElm = document.getElementById('#complete-toast');
       const toast = new bootstrap.Toast(toastElm)

       toast.show();
   },
   // ... other methods here
};

Then pull it into your Vue file like this:

@section scripts {

    <script>
        import { showCompleteToast } from "..path/to/toaster-message.js"
        const app = new Vue({
            el: "#app",
            data() {
                return {

                }
            },
            methods: {
                showToast: function(){
                   //I want to call the show toast from here
                   showCompleteToast();
                },
                submit: async function () {
                    try {

                        this.showToast();

                    } catch (error) {
                      
                        console.log(error)
                    }
                }
            }

        });
    </script>
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have this implemented on ASP .net application. But this didn't work I have tried this too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.