在 Nuxt 项目中哪里放置自定义 Vue mixins 和辅助函数?

Mar*_*tin 5 vue.js vuejs2 nuxt.js

我有一个 Nuxt 项目,我需要制作自定义函数和 Vue mixins 以在 Vue 组件和页面中重用。

In which folder should I put the files containing these? Should I create a 'lib' folder at the top level of the Nuxt project and put the files in there?

Extra details if that can help:

  • These functions will be imported only when needed (not global)
  • These functions will be tested

Nuxt Directory Structure Documentation

ham*_*kan 11

在我的 nuxt 项目中,我通常在项目的根目录级别添加四个文件夹,这些文件夹用于mixins我的所有 mixins、models我在整个应用程序中使用的模型或类,services其中包含我的所有 API 端点以及utils我的所有实用程序函数和其他通用函数,例如我的输入的验证函数和我的目录如下所示:

在此输入图像描述

在这种情况下,mixins您可以将所需的 mixin 导入到所需的组件中,并像平常一样使用它们:

import someMixin from '@/mixins/someMixin'
...
export default {
  mixins: [someMixin],
  ...
}
Run Code Online (Sandbox Code Playgroud)