【问题标题】:How to properly import component in nuxt.config.js to use as a custom icon?如何正确导入 nuxt.config.js 中的组件以用作自定义图标?
【发布时间】:2021-06-27 12:39:28
【问题描述】:

我正在尝试使用 SVG 文件作为自定义图标,在 nuxt.config.js 中使用此结构:

import UploadIcon from '@/components/icons/UploadIcon'

export default {
...
    vuetify: {
       customVariables: ['~/assets/variables.scss'],
       icons: {
           values: {
              upload: {
                  component: UploadIcon  <------here is my custom icon
              }
           }
       }
    }
 },

Nuxt 显示错误:

   │   ✖ Nuxt Fatal Error                                                          │
   │                                                                               │
   │   Error: Cannot find module '@/components/icons/UploadIcon'                   │
   │   Require stack:                                                              │
   │   - C:\Users\Admin\Documents\portfolio\artwork\artwork\nuxt.config.js         │

问题是 IDE 会自动进行这样的导入: import UploadIcon from '@/components/icons/UploadIcon'

而且它不起作用。 我尝试了什么:

  1. 在组件路径中使用~ 而不是@
  2. 路径的其他变体,包括绝对路径。

但是,当我尝试使用绝对路径时,它会显示这样的错误:

   ╭───────────────────────────────────────╮
   │                                       │
   │   ✖ Nuxt Fatal Error                  │
   │                                       │
   │   SyntaxError: Unexpected token '<'   │
   │                                       │
   ╰───────────────────────────────────────╯

所以我找不到方法,我查看了这些文档:

通常我一直在尝试遵循这个答案:https://stackoverflow.com/a/58563938/7017890 但是有一个常规的vuetify.js 正在使用配置文件。用 Nuxt 就不行了。

【问题讨论】:

    标签: nuxt.js vuetify.js


    【解决方案1】:

    首先我建议使用vuetify.options.js 配置文件,而不是从nuxt.config.js (@nuxtjs/vuetify documentation) 加载配置。

    在您的 vuetify.options.js 文件中(将其保存在顶级目录中

    // Note the lack of a leading slash (/)
    import myCustomIcon from "components/icons/UploadIcon";
    
    export default function () {
      return {
        // other vuetify options here,
        icons: {
          values: {
            upload: { component: myCustomIcon }
          }
        }
      };
    };
    

    【讨论】:

    • 完美,我什至没有考虑使用外部vuetify.options.js 文件。谢谢!
    猜你喜欢
    • 2021-01-20
    • 2021-01-20
    • 2019-05-13
    • 1970-01-01
    • 2021-11-08
    • 2013-07-21
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多