简体   繁体   English

vue过滤应该使用REST API还是url参数

[英]Should vue filtering use REST API or url parameters

I'm designing a website with a REST API using Django Rest Framework and Vue for the front end and I'm trying to work out what the proper way to do filtering is. I'm designing a website with a REST API using Django Rest Framework and Vue for the front end and I'm trying to work out what the proper way to do filtering is.

As far as I can see, I can either:-据我所知,我可以:-

a) Allow filtering via the API by using URL parameters like /?foo=bar a) 允许通过 API 使用 URL 参数(如 /?foo=bar)进行过滤

or或者

b) Do all the filtering on the Vue side by only displaying the items that are returned that have foo=bar b) 通过仅显示返回的具有 foo=bar 的项目在 Vue 端进行所有过滤

Are there any strong reasons for doing one over the other?是否有任何强有力的理由来做一个而不是另一个?

The real answer to this question is "it depends".这个问题的真正答案是“视情况而定”。

Here are a few questions to ask yourself to help determine what the best approach is:这里有几个问题要问自己,以帮助确定最佳方法是什么:

How much data will be returned if I don't filter at the API level?如果我不在 API 级别过滤,会返回多少数据?

If you're returning just a few records, there won't be a noticeable performance hit when the query runs.如果您只返回几条记录,则查询运行时不会对性能造成明显影响。 If you're returning thousands, you'll likely want to consider server side querying/paging.如果您返回数千,您可能需要考虑服务器端查询/分页。

If you're building an application where the amount of data will grow over time, it's best to build the server side querying from the get-go.如果您正在构建一个数据量会随着时间增长的应用程序,那么最好从一开始就构建服务器端查询。

What do I want the front-end experience to be like?我希望前端体验是什么样的?

For API calls that return small amounts of data, the user experience will be much more responsive if you return all records up front and do client-side filtering.对于返回少量数据的 API 调用,如果您预先返回所有记录并进行客户端过滤,用户体验将更加灵敏。 That way if users change filters or click through paged data, the UI can update almost instantaneously.这样,如果用户更改过滤器或单击分页数据,UI 几乎可以立即更新。

Will any other applications be consuming my API?任何其他应用程序会使用我的 API 吗?

If you plan to build other apps that consume the API, you may want to build the filtering at the API level so you don't need to recreate front-end filtering logic in every consuming application.如果您计划构建使用 API 的其他应用程序,您可能希望在 API 级别构建过滤,因此您无需在每个使用应用程序中重新创建前端过滤逻辑。

Hopefully these questions can help guide you to the best answer for your use case.希望这些问题可以帮助您为您的用例找到最佳答案。

Whenever I come across this issue I ask myself just one question: How many items are you working with?每当我遇到这个问题时,我都会问自己一个问题:你正在处理多少个项目? If you're only returning a few items from the API you can easily do the filtering on the front-end side and save yourself a bunch of requests whenever the results are filtered.如果您只从 API 返回几个项目,您可以轻松地在前端进行过滤,并在过滤结果时为自己节省一堆请求。 Also, if the result set is quite small, it's a lot faster to do it this way rather than sending off a request every time the filters change.此外,如果结果集非常小,这样做比每次过滤器更改时都发送请求要快得多。

However, if you're working with a very large number of items, it's probably best to just filter them out in the API, or even via your database query if that's what you're working with.但是,如果您正在处理大量项目,最好在 API 中将它们过滤掉,或者甚至通过数据库查询(如果您正在使用的话)。 This will save you from returning a large number of results to the front-end.这将使您免于将大量结果返回到前端。 Also, filtering large numbers of items on the front-end can significantly impact performance since it usually involves looping over a collection.此外,在前端过滤大量项目会显着影响性能,因为它通常涉及对集合进行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM