In vuejs3 app I retrieve data from db with axios in method, like :
<script>
import appMixin from '@/appMixin'
import app from './../../App.vue' // eslint-disable-line
import axios from 'axios'
const emitter = mitt()
export default {
name: 'adminCategoriesList',
mixins: [appMixin],
data: function () {
return {
categoriesPerPage: 20,
currentPage: 1,
categoriesTotalCount: 0,
categories: []
}
},
components: {
},
setup () {
const adminCategoriesListInit = async () => {
this.loadCategories() // ERROR HERE
}
function onSubmit (credentials) {
alert(JSON.stringify(credentials, null, 2))
console.log('this::')
console.log(this)
console.log('app::')
}
onMounted(adminCategoriesListInit)
return {
// schema,
onSubmit
}
}, // setup
methods: {
loadCategories() {
...
}
and I got error in browser's console :
Cannot read property 'loadCategories' of undefined
If to remove “this.” in loadCategories call I got error :
'loadCategories' is not defined
I need to make loadCategories as method, as I need to cal;l it from different places.
Which way is correct ?
Thanks!
Sometimes, we may want to call a Vue 3 component method from outside the component. In this article, we’ll look at how to call a Vue 3 component method from outside the component. We can assign a ref to a component and get the method from the ref object and call it. We assign the helloWorld ref to the HelloWorld component.
You can set ref for child components, after that call in parent via $refs. Add ref to the child component like this: Add click event to the parent like this: Here is the full example: Vue components are reusable instances with a name, so that is why they accept the same options as new Vue, data, computed, watch, methods.
In HelloWorld.vue , we have the greet method which calls this.$emit with the event name as the first argument and the event payload as the 2nd argument. Subsequent arguments are also event payloads which will be used as 2nd and subsequent arguments of the event handler method. Then in App.vue , listen to the greet event with the @greet directive.
Earlier we mentioned that Vue creates a global object in the background that puts the data properties and methods in the same scope. Let’s imagine that the methods option in the following example is the global object that Vue creates behind the scenes.
You could use composition and options api in the same component but for different properties and methods, in your case the data properties could be defined inside setup
hook using ref
or reactive
, the methods could be defined as plain js functions :
import {ref} from 'vue'
export default {
name: 'adminCategoriesList',
mixins: [appMixin],
components: {
},
setup () {
const categoriesPerPage= ref(20);
const currentPage=ref(1);
const categoriesTotalCount=ref(0),
const categories=ref[])
const adminCategoriesListInit = async () => {
loadCategories()
}
function onSubmit (credentials) {
alert(JSON.stringify(credentials, null, 2))
}
functions loadCategories(){
...
}
onMounted(adminCategoriesListInit)
return {
// schema,
onSubmit,
categoriesPerPage,
currentPage,
categoriesTotalCount,
categories
}
},
the properties defined by ref
could be used/mutated by property.value
and used in template like {{property}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With