Proper order of Pinia and Axios Calls when called in components.

huangapple go评论58阅读模式
英文:

Proper order of Pinia and Axios Calls when called in components

问题

我有一个Vue SPA,里面有很多数据表格和用户表单用于输入数据,这些数据被发送和从SQL数据库中提取。当我最初开发我的Vue应用程序时,我没有使用状态管理(Pinia)来存储任何数据,而是每次在组件挂载时调用axios查询,这会在组件通过vue-router重新加载时刷新。然而,我现在想利用Pinia存储,这样我可以在其中调用SignalR请求,以保持实时数据流到存储中。然而,在组件的初始加载中,我应该从哪里获取数据呢?我是否仍然应该在组件挂载时调用axios查询,还是在组件挂载时应该调用Pinia存储中执行axios查询的函数?还是Pinia存储有一种默认加载数据的方式吗?

我不确定Pinia、axios和初始/持续数据加载应该采用哪种正确的方法。

英文:

I have a Vue SPA I built that has a bunch of datatables and userforms for enter data and this data is sent and pulled from a sql db. When i originally developed my vue application I did not use state management (pinia) to store any data but instead I just would call a axios query everytime on mount of a component and this would refresh each time the component is reloaded via vue-router. However, I am trying to take advantage of Pinia stores now so I could technically also call signalr requests in them to keep live data flowing to the store. However, on the initial load of my components where should I get the data from? Should I call the axios queries in my component on mount still, or on mount of the component should I call a function in the pinia store that does the axios query? Or is there some way for the pinia store to load the data by default ?

I am not sure what the correct approach is for how pinia, axios, and initial/continuing data loading should take place.

答案1

得分: 2

这是一个基于观点的问题,但我认为每个开始使用Pinia与Vue的人都会问。不会有正确答案。我只想分享我认为对于Vue项目的良好结构是什么。

我总是将我的应用程序分为层次。每个层次通过接口与另一个层次通信,这样我们可以轻松地在不更新另一层中的任何代码的情况下切换库/API。

你可能之前听说过MVC设计模式,所以这种模式有点像它。我们有3个层次:视图状态管理API视图层次从状态管理层次获取数据。如果数据存在,状态管理层次返回它。如果数据不存在,状态管理调用API来检索它。

每个层次通过一个接口与另一个层次通信,在我们的情况下,它只是一个函数。假设你想构建一个产品页面,你想通过它的句柄获取产品。现在,在状态管理层次中,你定义一个类似这样的操作

// productStore.ts
getProductByHandle(handle: string): Product {
 return API.getProductByHandle(handle)
}

API层次中,你定义一个类似这样的函数:

// productAPI.ts
getProductByHandle(handle: string): Product {
 return fetch('product-api-endpoint')
}

所以无论你使用什么库,你都必须保持这些函数的形状保持不变。这里的形状是输入(handle)和输出(Product)。如果你能做到这一点,你就可以在不修改其他层次代码的情况下在Pinia/Vuexaxios/fetch或任何你想要的之间切换。

英文:

This is an opinion-based question, but I think everyone who starts using Pinia with Vue would ask. There will be no correct answer. I just want to share what I think is a good structure for a Vue project.

I always divide my application into tiers. Each tier communicates with another tier through an interface so we can easily switch between library/API without updating any code in the other tier.

   ---------------          --------------------          ---------------
   |     View    |          | State management |          |     API     |
   | ~~~~~~~~~~~ | <----->  | ~~~~~~~~~~~~~~~~ | <----->  | ~~~~~~~~~~~ |
   | .vue,       |          |   Pinia / Vuex   |          | axios/fetch |
   | composables |          |                  |          |             |
   |_____________|          |__________________|          |_____________| 

You might hear about the MVC design pattern before, so this pattern is something like it. We have 3 tiers View, State management, and API. The View tier gets data from the State Management tier. If the data exists, the State Management tier returns it. If the data does not exist, the State Management calls the API to retrieve it

Each tier communicates with another tier through an interface, in our case, it is just a function. Let says you want to build a product page, so you want to get the product by its handle. Now, in the State management tier, you define an action like this

// productStore.ts
getProductByHandle(handle: string) : Product {
 return API.getProductByHandle(handle)
}

In the API tier, you define a function like this:

// productAPI.ts
getProductByHandle(handle: string) : Product {
 return fetch('product-api-endpoint')
}

So no matter library you use, you have to keep the shape of these functions staying the same. The shape here is the input (handle) and the output (Product). If you can do it, you can switch between Pinia/Vuex, axios/fetch, or whatever you want without modifying other tier code

huangapple
  • 本文由 发表于 2023年6月9日 08:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定