Inertia: Access this.$inertia inside Composition API vue 3

Created on 16 Nov 2020  路  2Comments  路  Source: inertiajs/inertia

Hi people,

I'm new using inertia, so I get up an app with Laravel-Inertia-Vue3.

I've been playing around with the Composition API but I don't know how to access this.$inertia inside the set up method.

The code is:

<script>
import {reactive} from "vue";

export default {
    setup() {
        const data = reactive({
            form: {
                first_name: null,
                last_name: null,
                email: null,
            },
        });

        const submit = () => {
            this.$inertia.post('/form', data.form)
        }

        return {
            data,
            submit
        }
    }
}
</script>

But when calling the submit method I got Uncaught TypeError: _this is undefined.

question vue 3

Most helpful comment

When using the Vue 3 composition API, this is undefined within the setup() method. Meaning you cannot access this.$inertia. In these situations, you need to simply import Inertia instead.

import { Inertia } from '@inertiajs/inertia'

Inertia.post('/form', data.form)

All 2 comments

When using the Vue 3 composition API, this is undefined within the setup() method. Meaning you cannot access this.$inertia. In these situations, you need to simply import Inertia instead.

import { Inertia } from '@inertiajs/inertia'

Inertia.post('/form', data.form)

@reinink thanks a lot, now is working as expected.

Is there any difference in using Inertia.post('/form', data.form) over axios.post('/form', data.form) with the Composition API?

If I use this.$inertia on the Options API and for example I make dd(request()->all() on the server side, I got a modal with the result on the client side (see image).

Screen Shot 2020-11-16 at 14 10 36

But when I use Inertia.post('/form', data.form), there isn't modal I have to check out the response on devtools

Was this page helpful?
0 / 5 - 0 ratings

Related issues

reinink picture reinink  路  5Comments

JoeCianflone picture JoeCianflone  路  4Comments

Ollie1700 picture Ollie1700  路  4Comments

jalt007 picture jalt007  路  4Comments

sebastiandedeyne picture sebastiandedeyne  路  6Comments