We will always use the same endpoint because this is defined in the mixing. We cannot pass even the endpoint there, not speaking about replacing about replacing Axios with some kind of CraftQL calls. Conflict prone.
As I mentioned, mixing properties could be overwritten by component ones. In this case, we can't be sure that everything we exposed from the mixing safely landed in the component and unclear source. If you've ever used mixins, you know how hard it's to track which method comes from which mixing, especially if you have multiple swarms, multiple ones in the component.
We have another option, which is scope slots. I won't go into deep details, but basically in view, component can have slots, and we can place anything in this slot, like text, HTML, other components, and all this content will belong to parent scope. When we speak about scope slots, we mean that child component, in this case, it's generic search, can expose a part of its scope like a data, methods, whatever we want, to the content in the slot. So you can think about component with a scope slot, like a component that contains some logic, not a template in this case, like a wrapper that forms some operations and exposes data and methods to its content.
It looks much better because as you can see, we can pass run breed search there. So we are not setting it up to static access call, but this is not flexible. First of all, data methods that are exposed to the scope slot are exposed only to the scope slot. We cannot reuse any kind of exposed data in other places in the template or in the options API or whatever else outside of the slot, and this is less performant because in this case generic search is still a view component. This is a view instance and you need to mount this view instance to make it work. So do we have any other options in Vue 3? Yes. This is a new composition API, and I want to show you how does it work and how we can abstract these two features into the composition API. That's why we're switching to live demo. Wish me some luck.
So here is a code for the search and sorting, and let's start with abstracting search to its own composable. Let's go to composables folder and create a new file named search.js. And we want to export a function from here. Let's name it useSearch. And let's take a look. What do we have for search? First of all, we have two of these reactive properties, which are query and results. And we want to abstract them completely from this component. So let's create our first reactive property called searchQuery. And this will be a ref of an empty string. Empty string is an initial value of this search query here, and what ref does under the hood. In fact, we are creating a search query, which will be equal to an object with value of an empty string.
Comments