简体   繁体   English

如何在不同的组件中复用一个 SFC Vue 组件实例?

[英]How to reuse a SFC Vue component instance in different components?

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.由于耗资巨大,memory,并且在很多页面(root下的组件)中使用,所以我只想实例化一次,并在依赖它的每个页面中重用它。 Also, Player is resized for some pages, so I need it to be dynamically adjustable, which means it should support props.此外,Player 会针对某些页面调整大小,因此我需要它可以动态调整,这意味着它应该支持道具。 Furthermore, since there are many pages using Player, I hope I can use Provide / Inject feature of Vue to spreading Player everywhere.此外,由于有很多页面使用播放器,我希望我可以使用 Vue 的Provide / Inject功能来到处传播播放器。

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:总结一下,Player在我的想象中有这些特点:

  1. Initiated in root component.在根组件中启动。
  2. The root component Provide Player to every component under it.根组件Provide Player 给它下面的每个组件。
  3. Pages under root can pass props to the Injected Player somehow to resize it. root 下的页面可以以某种方式将道具传递给Injected的播放器以调整其大小。

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.我试图在 memory 启动播放器,但我不知道如何渲染它。
  • I tried to initiate Player as a dynamic component , but I don't know where to place the slot and how to initiate Player.我试图将Player 作为一个动态组件来启动,但我不知道在哪里放置插槽以及如何启动 Player。
  • I tried to understand render function to re-render it, but I don't know how to leverage it in my case.我试图了解 render function 以重新渲染它,但我不知道如何在我的案例中利用它。
  • This post seems to be the closest solution, but I am still confused how to implement the concept with limited description in it. 这篇文章似乎是最接近的解决方案,但我仍然对如何实现这个概念感到困惑,其中的描述有限。

There are two ways to do this-有两种方法可以做到这一点-

  1. Globally import the component in the main.js file and use on any page freely.全局导入main.js文件中的组件,任意页面自由使用。 ie- IE-
    import Vue from "vue";  
    import Player from "@/components/Player.vue";  
    Vue.component("AppButton", AppButton); 
  1. Create a mixin named playerMixin.js inside the mixins directory.mixins目录中创建一个名为playerMixin.js的 mixin。
    Import the Player component inside that mixin and import that mixin in all pages.将 Player 组件导入该 mixin 并在所有页面中导入该 mixin。
    The mixin code should look like this-混合代码应该是这样的——

playerMixin.js playerMixin.js

import Player from "@/components/Player.vue";

export const playerMixin = {
  data() {
    // any data properties related to player functionalities 
  },

  components: {
    Player,
  },

  methods: {
    // any methods related to player functionalities
  }
}

Now, you can use this mixin on any page, like this-现在,你可以在任何页面上使用这个 mixin,就像这样 -

AnyPage.vue AnyPage.vue

import { playerMixin} from "@/mixins/playerMixin.js";

export default {
  name: "AnyPage",
  mixins: [playerMixin],
}

The main benefit of using mixin is that not only components will be imported once but the common functionality of Player which is the same for every page like passing props, and methods like play, a pause can be written once and can be used by any page.使用 mixin 的主要好处是不仅组件会被导入一次,而且 Player 的通用功能在每个页面都是相同的,例如传递 props,以及像播放、暂停这样的方法,只需编写一次,就可以被任何页面使用.

To understand more about mixin, read this .要了解有关 mixin 的更多信息,请阅读此

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

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