How to use Web Components in React or Vue All In One
How to use Web Components in React or Vue All In One
Web Components 为可复用组件提供了强大的封装
https://developer.mozilla.org/en-US/docs/Web/Web_Components
React
class HelloMessage extends React.Component {
render() {
return <div>Hello <x-search>{this.props.name}</x-search>!</div>;
}
}
function BrickFlipbox() {
// ✅ class
return (
<brick-flipbox class="demo">
<div>front</div>
<div>back</div>
</brick-flipbox>
);
}
class XSearch extends HTMLElement {
connectedCallback() {
const mountPoint = document.createElement('span');
this.attachShadow({ mode: 'open' }).appendChild(mountPoint);
const name = this.getAttribute('name');
const url = 'https://www.google.com/search?q=' + encodeURIComponent(name);
const root = ReactDOM.createRoot(mountPoint);
root.render(<a href={url}>{name}</a>);
}
}
customElements.define('x-search', XSearch);
https://zh-hans.reactjs.org/docs/web-components.html
Vue
跳过自定义元素
组件的解析
// 仅当使用浏览器内编译时有效
// 如果你正在使用构建工具,请查看下方的配置示例
app.config.compilerOptions.isCustomElement = tag => tag.includes('-')
// vite.config.js
import vue from '@vitejs/plugin-vue'
export default {
plugins: [
vue({
template: {
compilerOptions: {
// 将所有包含短横线的标签作为自定义元素处理
isCustomElement: tag => tag.includes('-')
}
}
})
]
}
// vue.config.js
module.exports = {
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
.tap(options => ({
...options,
compilerOptions: {
// 将所有以 ion- 开头的标签作为自定义元素处理
isCustomElement: tag => tag.startsWith('ion-')
}
}))
}
}
https://v3.cn.vuejs.org/guide/web-components.html
https://vuejs.org/guide/extras/web-components.html
https://staging-cn.vuejs.org/guide/extras/web-components.html
https://github.com/vuejs/petite-vue
test
Custom Elements Everywhere 测试
⭐️⭐️⭐️
Version: 18.2.0
Browser: Chrome Headless 103.0.5060.114 (Linux x86_64)
Timestamp: 7/21/2022, 9:07:53 PM
15 tests / 0 errors / 7 failures / 0 skipped / runtime: 0.082s
https://custom-elements-everywhere.com/libraries/react/results/results.html
⭐️⭐️⭐️⭐️
Browser: Chrome Headless 103.0.5060.114 (Linux x86_64)
Timestamp: 7/21/2022, 8:42:47 PM
15 tests / 0 errors / 3 failures / 0 skipped / runtime: 0.329s
https://custom-elements-everywhere.com/libraries/vue/results/results.html
Web Components
UI Library
https://github.com/xgqfrms/wcui
https://github.com/XboxYan/xy-ui
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16522064.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-07-26 uni-app 不支持 localStorage All In One
2021-07-26 同比 & 环比 All In One
2021-07-26 uni-app & icon-font All In One
2021-07-26 uni-app 性能优化 All In One
2021-07-26 CSS line-height All In One
2021-07-26 uni-app CSS 样式布局错乱 All In One
2020-07-26 JavaScript Inheritance All in One