Skip to content
Advertisement

Two of my routes are extremely similar, to the point that I copy-pasted most of the stuff. I assume I need to DRY the code but I’m not sure how

I have an admin panel route with 2 nested child routes that render 2 views – AdminBrands renders brands and allow the admin to see and delete brands, and AdminCategories where the admin can see and delete categories. Now the problem is that those 2 views are super similar to each other, but at the same time they have got some differences, so I’m not exactly sure how to DRY them up or even if I should.

The template is mostly the same, with just a couple of small differences. The script part is literally identical, except that the data and functions have different names – getCategories() and getBrands() for example. Should I try creating some all-mighty view that changes depending on the current route, or stick to having more than 1 components? I’ve given 2 views as my example, but in my project, I actually have 5 views that are extremely similar, so creating a single view would allow me to remove 4 of them which will make the folder structure a lot cleaner.

JavaScript

AdminBrands view:

JavaScript

AdminCategories view:

JavaScript

Advertisement

Answer

For now, the best solution is to abstract completely identical (common) functions with Vue Mixins such as setCurrentPage and most of your data points currentPage, totalItems and totalPages. Furthermore, you can abstract very similar functions, such as getCategories/getBrands and deleteCategory/deleteBrand. Here it’s best if you abstract each pair into one function, taking an input whether it’s about categories or brands, and then working with them.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement