6

I'm currently using VueJS on an exiting Flask (Server-rendered) Homepage. I want to import a VueJS Component with the script-tag. The VueJS-Component should access to an API-Function from a third JS-file. The Cue-Component-File looks like this:

import {getContactsForNewMessageAPI} from "../../api/contactsAPI";

Vue.component('new-message-modal', {
    data: function () {
        return {
            selectFieldOptions: [],
            //... further attributes
        }
    },
    methods: {
        async getContacts(){
            this.selectFieldOptions = await getContactsForNewMessageAPI();
        },
        //... further methods
    }
});

Option 1:

In a template file I'm able to import a VueJS-Component like this:

<script type="text/javascript" src="js/ui/newMessageVueComponent.js"></script>

But if I import the component with the type="text/javascript", I can't use a function from an extra api.js file inside of the component like this:

import {getContactsForNewMessageAPI} from "../../api/contactsAPI";

The console prints the error:

Uncaught SyntaxError: Cannot use import statement outside a module

Option 2

Second option I tried is to import the component with type="module" like this:

<script type="module" src="js/ui/newMessageVueComponent.js"></script>

So I'm able to use the getContactsForNewMessageAPI() function, but the VueJS-component won't be rendered anymore. The following error occurs:

Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

Has anyone a solution for this?

7
  • 2
    You need a build system which will package all your frontend code and then you can import it via option 1 Commented Dec 13, 2019 at 10:02
  • is there no other way except packing all my js-stuff with e.g. Webpack? Commented Dec 13, 2019 at 10:14
  • Nope. In my opinion using a build system makes the process a lot more smoother. Commented Dec 13, 2019 at 10:18
  • You either define the component before Vue initialization, or include the component inside Vue initialization. Commented Dec 13, 2019 at 10:20
  • @ChristosLytras the component is defined before VueJS is initialized Commented Dec 13, 2019 at 10:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.