vite+vue使用自定义元素控制台警告Failed to resolve component

1,283 阅读1分钟

Snipaste_2023-12-13_10-41-50.png

当使用自定义元素时,控制台会发出这样的警告:Failed to resolve component: swiper-slide If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.

当然这也并不影响代码的运行,但是作为程序员,我们必须要严谨。那么我们应该如何去除这个警告呢?

只需要给vite.config.js文件添加一个配置:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
    plugins: [
        vue({
            template: {
                compilerOptions: {
                    // my-custom-element 是我们自定义的元素
                    isCustomElement: (tag) => ['my-custom-element'].includes(tag)
                }
            }
        })
    ]
})