首页 > 其他分享 >Vue3忽略自定义模板组件提示

Vue3忽略自定义模板组件提示

时间:2022-12-11 18:55:05浏览次数:72  
标签:vue 自定义 tag Vue Vue3 compilerOptions config 模板

为了在 Vue 应用程序中使用自定义元素库, 必须修改应用程序以定义自定义元素和通知 Vue 编译器在编译过程中忽略哪些元素。

根据同一页面,这可以通过修改 Vue 实例的配置来实现,如下所示:

Vue.config.ignoredElements = [/test-\w*/];

然而,这是Vue 2 的写法

对于 Vue 3,您必须使用 isCustomElement,如下所示

app.config.compilerOptions.isCustomElement = tag => /gc-\w*/.test(tag)

但这会导致 Vue 在控制台中抛出以下警告:

[Vue warn]: The `compilerOptions` config option is only respected when using a build of Vue.js that includes the runtime compiler (aka "full build"). Since you are using the runtime-only build, `compilerOptions` must be passed to `@vue/compiler-dom` in the build setup instead.
- For vue-loader: pass it via vue-loader's `compilerOptions` loader option.
- For vue-cli: see https://cli.vuejs.org/guide/webpack.html#modifying-options-of-a-loader
- For vite: pass it via @vitejs/plugin-vue options. See https://github.com/vitejs/vite/tree/main/p

指示 Vue 忽略组件
来源: https://stackoverflow.com/questions/69119951/using-stencil-components-with-ionic-vue

默认情况下,Vue 将尝试将非原生 HTML 标签解析为已注册的 Vue 组件,然后再回退到将其渲染为自定义元素。

这将导致 Vue 在开发过程中发出“无法解析组件”警告。

为了让 Vue 知道某些元素应该被视为自定义元素并跳过组件解析,我们可以指定compilerOptions.isCustomElement

如果你在构建设置中使用 Vue,该选项应该通过构建配置传递,因为它是一个编译时选项。

示例浏览器内配置

// Only works if using in-browser compilation.
// If using build tools, see config examples below.
app.config.compilerOptions.isCustomElement = (tag) => tag.includes('-')

示例 Vite 配置

// vite.config.js
import vue from '@vitejs/plugin-vue'

export default {
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // treat all tags with a dash as custom elements
          isCustomElement: (tag) => tag.includes('-')
        }
      }
    })
  ]
}

示例 Vue CLI 配置

// vue.config.js
module.exports = {
  chainWebpack: config => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => ({
        ...options,
        compilerOptions: {
          // treat any tag that starts with ion- as custom elements
          isCustomElement: tag => tag.startsWith('ion-')
        }
      }))
  }
}

标签:vue,自定义,tag,Vue,Vue3,compilerOptions,config,模板
From: https://www.cnblogs.com/echohye/p/16974168.html

相关文章

  • 小程序模板与配置
    WXML模板语法WXSS模板样式全局配置页面配置网络数据请求 WXML模板语法数据绑定在data中定义数据在WXML中使用数据Mustache语法(双大......
  • 小程序自定义组件和 npm包的使用
    自定义组件创建自定义组件在根目录下创建components文件夹下创建自定义的组件引用自定义组件//在页面的.json文件中,引入组件{  "usingComponents":{ ......
  • 算法总结-二分查找(两种模板方法总结)
    【二分查找】定义:二分查找也称折半查找(BinarySearch),是一种在有序数组中查找某一特定元素的搜索算法。我们可以从定义可知,运用二分搜索的前提是数组必须是有序的,这里需要......
  • vue3
    01-为什么学vue3目标:了解vue3现状,以及它的优点,展望它的未来Vue3现状:vue-next(opensnewwindow)2020年09月18日,正式发布vue3.0版本。但是由于刚发布周边生态不支持,......
  • 【ASP.NET Core】MVC控制器的各种自定义:IActionHttpMethodProvider 接口
    IActionHttpMethodProvider接口的结构很简单,实现该接口只要实现一个属性即可——HttpMethods。该属性是一个字符串序列。这啥意思呢?这个字符串序列代表的就是受支持的HT......
  • P5410 【模板】扩展 KMP(Z 函数)
    题目链接P5410【模板】扩展KMP(Z函数)【模板】扩展KMP(Z函数)题目描述给定两个字符串\(a,b\),你要求出两个数组:\(b\)的\(z\)函数数组\(z\),即\(b\)与\(b\)的......
  • C++:类模板知识回顾
    概述类的私有、公有、类继承有时并不能满足我们的开发需求,尤其是将类作为容器(泛型编程)使用时,因此类模板在C++随之衍生。相关概念也会在下文中一一阐述~模板类的定义与使......
  • Vue3学习笔记(四)——组件、生命周期、Hook
    一、组件如果我们将一个页面中所有的处理逻辑全部放在一起,处理起来就会变得非常复杂,而且不利于后续的管理以及扩展,但如果,我们将一个页面拆分成一个个小的功能块,每个功能块完......
  • 模板链表类的扩展(QListEx<T>)
    以前写的链表都是比较简单的,插入节点是在头节点上,所以循环链表时都是从最后一个数据往前找的,给人的感觉是倒着的,今天写一个在链表尾部插入数据1。链表节点类的定义/链......
  • VUE3 API之reactive和ref常见问题解决
    reactive解构最深的一层,失去响应性问题<scriptsetuplang="ts">lettarget={a:{b:1}};lettarget1={c:1};constobj=reactive(target)constobj1=......