21

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!

3
  • 1
    You want to return the method loadCategories from the setup. Also, I'd suggest doing either full Composition API or Options API -- but not both. Commented Nov 9, 2020 at 5:41
  • I prefer Composition API , but in which way ? What is wrong in my structure now? Commented Nov 9, 2020 at 5:43
  • 1
    Well, try placing the method inside the setup callback and return it from there. That allows you to access it as well as expose it on the template (once returned). Commented Nov 9, 2020 at 5:47

1 Answer 1

20

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}}

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

2 Comments

Thanks! Please more details about ref with link where it is described ?
you're welcome, i added the link to the official docs, and watch this video

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.