1

How can we call Vue Methods and use Vue Variables in JavaScript?

Below Example, calling test() or get value of any variable

I am rendering my page using InertiaJS.

<script>
import Layout from '@/Shared/Layout'

export default {
  metaInfo: { title: 'Create User' },
  layout: Layout,
  props: {
    errors: Object,
  },
  remember: 'form',
  data() {
    return {
      sending: false,
      form: {
        first_name: null,
        last_name: null,
        email: null,
        password: null,
        owner: false,
        photo: null,
      },
    }
  },
  methods: {
    test() {
      //do something here
      console.log('test');
    },
  },
}

// Calling this should call Vue Method. But It's not working
test();

</script>

1 Answer 1

1

If you use Vue.js you need to call your method, test() in the <template> part with an event, like @click="test()" for instance. And I prefer to write method, like this : test: function () {...}.

<div @click="test">...</div> because you don't have parameter for test()

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

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.