1

I have a vuejs component (map.vue) like this:

<script> 
import '../../assets/js/map.js' 

export default {
    name: 'home',
    data () {
        return {
            init_data: {},
        }
    },
    created: function() {
         this.init_data = window.get_init_data(this.view, function(response) {
               document.title = response.body.page_title;
               init_map(some_arguments);
         });
    }
}
</script>

map.js:

const key = ******;
function init_map(some_args) {
    ...
}

Error:

[Vue warn]: Error in created hook: "ReferenceError: init_map is not defined"

And in fact, inspecting the source code the function it's been called before it's signature.

Note: I don't want to include map.js in webpack entries because i only need this script in one component (map.vue).

4
  • try require( '../../assets/js/map.js') Commented Jun 29, 2017 at 12:13
  • Already did @OlegShleif , the problem remains. Thanks Commented Jun 29, 2017 at 12:14
  • It would not be difficult to explain how? Commented Jun 29, 2017 at 12:15
  • @OlegShleif, to explain how about what? Commented Jun 29, 2017 at 12:16

1 Answer 1

1

Try this

test.js

export function writeHello() {
    console.log('hello');
}

*****.vue

import {writeHello} from './../test.js';

export default {
    created() {
        writeHello();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any difference between export and export default? Both worked
@HulaHula export is where you name a class or function (or multiples) which can then be imported by specifying the definition with curly braces as in the above. export default means that if no exact definition is supplied it will automatically return the default when importing. an example might be import foo from '/path/to/bar' where foo becomes the default export.

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.