首页 > 解决方案 > 我的函数应该在 /mixins 还是 /plugins 中?

问题描述

例如,如果我有一个正在使用的外部包,vue-js-modal. 我将其导入/plugins/vue-js-modal.js

import Vue from 'vue'
import VModal from 'vue-js-modal'

Vue.use(VModal)

现在假设我有一个函数,vue-js-modal我想在下面的一堆不同组件中使用它:

function showHideModal(showModalName = null, hideModalName = null) {
    if (hideModalName) {
        this.$modal.hide(hideModalName)
    }
    if (showModalName) {
        this.$modal.show(showModalName)
    }
}

这个函数应该存储在它自己的文件中/plugins/vue-js-modal.js还是应该放入它自己的/mixins/showHideModal.js文件中?这里哪个更合适,为什么?

标签: javascriptvue.jsnuxt.js

解决方案


According to plugins document in Nuxt.js website:

Nuxt.js allows you to define JavaScript plugins to be run before instantiating the root Vue.js Application. This is especially helpful when using Vue libraries, external modules or your own plugins.

So if you want to use external modules it's better to have them stored/initiated in plugins, Whereas if you want to have reusable functions for components, you use Mixins which then "Mix" the function and the component together.

Read more about mixins here.


推荐阅读