Motivation:
I have a single file component, say Player, which displays video or show some images. Since it costs huge amount of memory, and it is used in many pages (components under root), I want to instantiate it only once and reuse it in every page depending on it. Also, Player is resized for some pages, so I need it to be dynamically adjustable, which means it should support props. Furthermore, since there are many pages using Player, I hope I can use Provide / Inject feature of Vue to spreading Player everywhere.
Current:
// Player
<template>
<div>
... // display images and video
</div>
</template>
<script>
export default Vue.extend({
props: {
width: {
type: Number,
default: 284,
},
height: {
type: Number,
default: 214,
},
},
data() {
... // images and video
},
methods: {
... // logic related to images and video
},
})
// An example of `pages`
<template>
<div>
<player
:width="592"
:height="333"
/>
... // some other logic
</div>
</template>
... // everything else
Expect:
To sum up, Player has these features in my imagination:
- Initiated in root component.
- The root component
ProvidePlayer to every component under it. - Pages under root can pass props to the
InjectedPlayer somehow to resize it.
This is just my imagination, any other available solution is welcome.
What I've tried:
- I tried to initiate Player in memory, but I don't know how to render it.
- I tried to initiate Player as a dynamic component, but I don't know where to place the slot and how to initiate Player.
- I tried to understand render function to re-render it, but I don't know how to leverage it in my case.
- This post seems to be the closest solution, but I am still confused how to implement the concept with limited description in it.
Although here we are using a single reactive object as a store, you can also share reactive state created using other Reactivity APIs such as ref() or computed(), or even return global state from a ComposableIn Vue 2, mixins were the primary mechanism for creating reusable chunks of component logic. While mixins continue to be supported in Vue 3, Composition API is now the preferred approach for code reuse between components.