偶然发现vue的文档中的有这样一个api:defineCustomElement
尝试了很久,才明白用法,在这里分享一下:
1.修改配置
vite.config.js
1 | plugins: [ |
这里是vite的配置,其他方式参考官网
2.创建自定义元素
- 方式一
新建list/index.js
1 | import { defineCustomElement } from 'vue' |
- 方式二
新建list/ui.ce.vue,文件内容如下:
1 | <template> |
在`list/index.js`中引入:
1 | import { defineCustomElement } from 'vue' |
注册组件
main.js
1 | import MyVueElement from "./views/defineCustomElement" |
使用
- 直接在
vue或者html文件中使用
1 | <my-vue-element title="方式一"></my-vue-element> |
- 在
js中
1 | document.body.appendChild( |
虽然大概了解了这个api的用法,还有些疑惑
app.config.compilerOptions.isCustomElement = (tag) => tag.includes('-')这个配置在什么场景生效使用场景有哪些