3

A vue file in my project has become too big. I wanted to seperate its script section into a js file. In js file, I export the Vue object;

export default new Vue({
  data: {
    search: "",
    ...

And import it in the Vue file;

<script src="./RecordNameTable.js">
</script>

But this is not working, it gives errors. I can import it as a .vue file which has the classic Vue syntax between <script></script> tags. But I do not want this. How to solve this problem?

6
  • 1
    try to replace export default with module.exports = new Vue({... and can you share the errors that shows? Commented Sep 12, 2019 at 11:14
  • Is it a single file component with .vue extension? Commented Sep 12, 2019 at 11:28
  • @GhyathDarwish this error thrown when module.exports is used; Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' Commented Sep 12, 2019 at 11:34
  • @PrisonerRaju yes Commented Sep 12, 2019 at 11:34
  • You may try to force type="module" on your script tag. Doc: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules Commented Sep 12, 2019 at 11:35

4 Answers 4

4

You have to export an object from your external script, Not a Vue instance.

Here is a simple solution

App.vue

<template>
    <div>
        {{ message }}
    </div>
</template>

// This will work
<script src="./app.js"></script>

// this will work too
<script>
    import App from './app.js';

    export default App;
</script>

app.js

export default {
    data(){
        return {
            message: "Hello world"
        };
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

but the import style i want is given in the docs, just there is no info about the content of the script file; vuejs.org/v2/guide/…
Yes, This is not mentioned in doc. You have to export an object from your external script, Not a Vue instance.
@dreyzz Please accept the most appropriate answer if your problem is solved :)
1

You solve the problem by dropping the "But I do not want this." attitude and simply use the only available solution at this point:

<script>
    import Obj from 'location';

    export default Obj;
</script>

2 Comments

but the import style i want is given in the docs, just there is no info about the content of the script file; vuejs.org/v2/guide/…
It tells you on the site that each page / component must return an object from <script>, if one's being used.
1

For vue 3 using Object Destructuring

<script>
import File_script from "./file_script.js";

export default {...File_script}
</script>

Comments

0

If it's become too big the correct approach is to break your template down into further components, not simply move the problem (and making it worse) by moving the script section to another file. You're approaching this wrong.

2 Comments

But in angular, even css is in a separate file. Are they doing it wrong?
Both frameworks are doing what they feel is best. I happen to agree with Vue's approach. Have a read of the docs, having the content together does not violate SRP. I'm saying that using a framework and then not following its patterns is what's going wrong here. vuejs.org/v2/guide/single-file-components.html @dreyzz

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.