How to structure api calls in Vue.js?

前端 未结 3 1773
长情又很酷
长情又很酷 2021-01-30 06:38

I\'m currently working on a new Vue.js application. It depends heavily on api calls to my backend database.

For a lot of things I use Vuex stores because it manages sha

3条回答
  •  故里飘歌
    2021-01-30 07:20

    Based on concept of Belmin Bedak`s answer, i have wrapped it all into a simple library:

    https://github.com/robsontenorio/vue-api-query

    You can request your API like this:

    All results

    // GET /posts?filter[status]=ACTIVE
    
    let post = await Post
      .where('status', 'ACTIVE')
      .get()
    

    Specific result

    // GET /posts/1
    
    let post = await Post.find(1)
    

    Editing

    // PUT /posts/1 
    
    post.title = 'Awsome!'
    post.save()
    

    Relationships

    // GET /users/1
    let user = await User.find(1)
    
    // GET users/1/posts
    let posts = await user
      .posts()
      .get()
    

提交回复
热议问题