Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ederson's avatar

Vue3 composable method not found

I thought i'd give composables a try following a few tutorials including J. Way's videos here In case it matters i use inertiajs

the task is simple. The controller returns an inertia page with a url as a prop. On mount i call a method defined in an composable with the prop as parameter

the page :

<script setup>
import usePagination from "../Use/usePagination.js";
const { loadData, list} = usePagination();
defineProps({ apiLink: String });
</script>

<template>
	
</template>
<script>
export default {
	methods: {},
	mounted() {
		this.loadData(this.apiLink);
	},
	data() {
		return {};
	},
};
</script>

the usePagination composable

import { ref, computed } from "vue";

export default function () {
    
    var list=ref()
    function loadData(url) {
       
        axios.get(url).then((response) => {                        
            this.list = response.data.data;
        });
    }
   
    return {
        loadData,
        list
    }
}

i get the following error

TestPage.vue:88 Uncaught (in promise) TypeError: this.loadData is not a function

when i use the same function in a click event it works but not when is called in the mounted section.

there is something obvious here that i am missing

0 likes
8 replies
Sergiu17's avatar

this should do, call the function without this

mounted() {
		loadData(this.apiLink);
},
ederson's avatar

thanks @Sergiu17 but i ve already done that

the erros message changes

Uncaught (in promise) ReferenceError: loadData is not defined
Maximegoncalves's avatar
Level 2

replace loadData(this.apiLink); with loadData(apiLink);

edit:

you need:

<script setup>
import usePagination from "../Use/usePagination.js";
const { loadData, list} = usePagination();
const props = defineProps({ apiLink: String });

onMounted(() => {
loadData(props.apiLink)
})
</script>

ederson's avatar

@Maximegoncalves the exception is thrown before the apiLink var is accessed. besides the error mentions the function not its parameters. Anyway out of frustration i tried it without success

ederson's avatar

i tried the modified answer (importing onMounted in the set of the page)

the method is now called but then in the composable list is not found

Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'list')
ederson's avatar

i moved the setup in the main script and somehow now works

lombervid's avatar

@ederson But why did you have 2 scripts in the first place? And one with Composition API and the other with, what seems to be, Options API?

ederson's avatar

@lombervid because i did not know any better :) once i made it work i searched a bit more and realized where my mistake was

Please or to participate in this conversation.