1

I'm defining the Vue.js instance like this in app.js:

const vm = new Vue({
    el: '#app',
    data: {
        page: 'welcome'
    }
});

and using Laravel's default webpack script:

mix.js('resources/assets/js/app.js', 'public/js')
  .sass('resources/assets/sass/app.scss', 'public/css');

Vue is loading correctly as the components are displaying and the Vue developer plugin is showing appropriate data.

However, in the developer console and in scripts on the page vm is not defined.
For instance on the page that is loading a Vue component

<script>
  console.log(vm); // Error: vm is not defined.
</script>

How do I access the Vue instance? I want to set a one of its data properties.

2 Answers 2

3

To have flexibility and import with any name, you can use default exports.

In app.js:

export default new Vue({
    el: '#app',
    data: {
        page: 'welcome'
    }
});

In other .js files:

import whateverName from './app';

Naturally, adjusting the ./app depending on where the file is at.

Sign up to request clarification or add additional context in comments.

1 Comment

I like your generalized answer better than mine.
3

I was able to use

export {
  vm
};

require('./main');

and in main.js

import {vm} from './app';

Comments

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.