javascript - 我的函数应该在 /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
文件中?这里哪个更合适,为什么?
解决方案
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.
推荐阅读
- python - 用于视频处理的 Argparse 语法
- amazon-web-services - 防止用户自行使用联合身份提供商 (FIP) 注册,但如果管理员添加,则允许使用 FIP 登录
- oracle19c - oracle.jdbc.OracleDatabaseException: ORA-01722: 无效号码
- python - Pycharm在放入并从队列中获取后丢失对象指示符
- jquery - Flexslider - 在最后一张幻灯片上禁用滚动
- reactjs - 2个问题,我似乎无法摆脱每个孩子应该有一个id错误
- abap - CL_GUI_ALV_GRID 可编辑字段不刷新
- spartacus-storefront - 延迟加载的覆盖或自定义 CMS 组件
- excel - Excel——找出财务季度的差异
- c++ - 给出超出范围的错误代码块的 Visual Studio 不会