Varun Verma
Varun Verma

Reputation: 11

Inertia JS, Calling Vue methods in JavaScript

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>

Upvotes: 1

Views: 1676

Answers (1)

zerbene
zerbene

Reputation: 1492

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()

Upvotes: 1

Related Questions