[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$KiqIp5Qu8t":3,"vue-tips-\u002Fvue-tips\u002Fdefining-and-registering-vue-web-components":52,"blogToc-\u002Fvue-tips\u002Fdefining-and-registering-vue-web-components":394,"vue-tips-surrounding-\u002Fvue-tips\u002Fdefining-and-registering-vue-web-components":616},{"id":4,"extension":5,"items":6,"meta":49,"stem":50,"__hash__":51},"menu\u002Fmenu.json","json",[7,12,16,20,24,28,32,36,41,45],{"i18nId":8,"to":9,"ariaLabel":10,"mainNavigation":11},"components.menu.home","\u002F","Home",true,{"i18nId":13,"to":14,"ariaLabel":15,"mainNavigation":11},"components.menu.about","\u002Fhire-me","About",{"i18nId":17,"to":18,"ariaLabel":19,"mainNavigation":11},"components.menu.testimonials","\u002Ftestimonials","Testimonials",{"i18nId":21,"to":22,"ariaLabel":23},"components.menu.contact","\u002Fcontact","Contact",{"i18nId":25,"to":26,"ariaLabel":27,"mainNavigation":11},"components.menu.blog","\u002Fblog","Blog",{"i18nId":29,"to":30,"ariaLabel":31,"mainNavigation":11},"components.menu.tips","\u002Fvue-tips","Vue Tips",{"i18nId":33,"to":34,"ariaLabel":35,"mainNavigation":11},"components.menu.projects","\u002Fprojects","Projects",{"i18nId":37,"to":38,"ariaLabel":39,"mainNavigation":40},"components.menu.stats","\u002Fstats","stats",false,{"i18nId":42,"to":43,"ariaLabel":44,"mainNavigation":40},"components.menu.businessCard","\u002Fbusiness-card","Business Card",{"i18nId":46,"to":47,"ariaLabel":48,"mainNavigation":40},"components.menu.publications","\u002Fpublications","Publications",{},"menu","njLT1QoRt2Dv8QWoVmH8xdTAiDywSj0bxBQdmoyWUE0",{"id":53,"title":54,"body":55,"categories":376,"cover":379,"date":380,"description":61,"extension":381,"lastUpdated":382,"meta":383,"navigation":11,"ogImage":382,"path":384,"readingTime":385,"robots":382,"schemaOrg":382,"seo":390,"sitemap":391,"stem":392,"tags":382,"__hash__":393},"vueTips\u002Fvue-tips\u002Fdefining-and-registering-vue-web-components\u002Findex.md","Vue Tip: Defining and Registering Vue Web Components",{"type":56,"value":57,"toc":371},"minimark",[58,62,68,71,76,84,220,231,240,244,255,287,291,294,356,367],[59,60,61],"p",{},"Web Components is an umbrella term for a set of web native APIs that allows developers to create reusable custom elements. Vue has excellent support for both consuming and creating custom elements.",[63,64,65],"note",{},[59,66,67],{},"The main advantage of web components is that they can be used with any framework or even without a framework.",[59,69,70],{},"In three simple steps, let's look at how we can create a web component from a Vue SFC (single-file component).",[72,73,75],"h3",{"id":74},"_1-create-the-custom-element","1. Create the custom element",[59,77,78,79,83],{},"To create the custom element, we use ",[80,81,82],"code",{},"defineCustomElement",":",[85,86,93],"pre",{"className":87,"code":88,"highlights":89,"language":91,"meta":92,"style":92},"language-js shiki shiki-themes github-light poimandres poimandres","import { defineCustomElement } from 'vue'\nimport Example from '.\u002FExample.ce.vue'\n\nconsole.log(Example.styles) \u002F\u002F [\"\u002F* inlined css *\u002F\"]\n\n\u002F\u002F convert into custom element constructor\nconst ExampleElement = defineCustomElement(Example)\n",[90],7,"js","",[80,94,95,130,147,153,185,190,196],{"__ignoreMap":92},[96,97,100,104,108,112,115,119,123,127],"span",{"class":98,"line":99},"line",1,[96,101,103],{"class":102},"sRZEA","import",[96,105,107],{"class":106},"sygvE"," {",[96,109,111],{"class":110},"srga6"," defineCustomElement",[96,113,114],{"class":106}," }",[96,116,118],{"class":117},"sOygg"," from",[96,120,122],{"class":121},"srEIG"," '",[96,124,126],{"class":125},"skTdI","vue",[96,128,129],{"class":121},"'\n",[96,131,133,135,138,140,142,145],{"class":98,"line":132},2,[96,134,103],{"class":102},[96,136,137],{"class":110}," Example",[96,139,118],{"class":117},[96,141,122],{"class":121},[96,143,144],{"class":125},".\u002FExample.ce.vue",[96,146,129],{"class":121},[96,148,150],{"class":98,"line":149},3,[96,151,152],{"emptyLinePlaceholder":11},"\n",[96,154,156,160,163,167,170,173,175,178,181],{"class":98,"line":155},4,[96,157,159],{"class":158},"sMzB8","console",[96,161,162],{"class":106},".",[96,164,166],{"class":165},"sHQ3f","log",[96,168,169],{"class":106},"(",[96,171,172],{"class":158},"Example",[96,174,162],{"class":106},[96,176,177],{"class":158},"styles",[96,179,180],{"class":106},") ",[96,182,184],{"class":183},"s1MN7","\u002F\u002F [\"\u002F* inlined css *\u002F\"]\n",[96,186,188],{"class":98,"line":187},5,[96,189,152],{"emptyLinePlaceholder":11},[96,191,193],{"class":98,"line":192},6,[96,194,195],{"class":183},"\u002F\u002F convert into custom element constructor\n",[96,197,200,204,208,211,213,215,217],{"class":198,"line":90},[98,199],"highlight",[96,201,203],{"class":202},"slWGy","const",[96,205,207],{"class":206},"sGRIV"," ExampleElement",[96,209,210],{"class":202}," =",[96,212,111],{"class":165},[96,214,169],{"class":106},[96,216,172],{"class":158},[96,218,219],{"class":106},")\n",[59,221,222,223,226,227,230],{},"You may have noticed that our SFC uses the special file ending ",[80,224,225],{},".ce.vue",". It inlines the SFC's ",[80,228,229],{},"\u003Cstyle>"," tags as strings of CSS and exposes them under the component's styles option.",[59,232,233,234,236,237,239],{},"During production build with default tooling setup, the ",[80,235,229],{}," inside the SFCs are extracted and merged into a single CSS file. But for the custom component, the ",[80,238,229],{}," tags should be injected into the custom element's shadow root.",[72,241,243],{"id":242},"_2-register-the-custom-element-with-the-dom","2. Register the custom element with the DOM",[59,245,246,247,250,251,254],{},"Next, we can register the custom element using the ",[80,248,249],{},"define"," method. After registration, all ",[80,252,253],{},"\u003Cmy-example>"," tags on the page will be upgraded afterward:",[85,256,258],{"className":87,"code":257,"language":91,"meta":92,"style":92},"customElements.define('my-example', ExampleElement)\n",[80,259,260],{"__ignoreMap":92},[96,261,262,265,267,269,271,274,277,279,282,285],{"class":98,"line":99},[96,263,264],{"class":158},"customElements",[96,266,162],{"class":106},[96,268,249],{"class":165},[96,270,169],{"class":106},[96,272,273],{"class":121},"'",[96,275,276],{"class":125},"my-example",[96,278,273],{"class":121},[96,280,281],{"class":106},", ",[96,283,284],{"class":158},"ExampleElement",[96,286,219],{"class":106},[72,288,290],{"id":289},"_3-use-the-custom-element-in-your-html","3. Use the custom element in your HTML",[59,292,293],{},"Finally, we can use our custom element in HTML:",[85,295,300],{"className":296,"code":297,"highlights":298,"language":299,"meta":92,"style":92},"language-html shiki shiki-themes github-light poimandres poimandres","\u003Chtml>\n  \u003Cbody>\n    \u003Cmy-example>\u003C\u002Fmy-example>\n  \u003C\u002Fbody>\n\u003C\u002Fhtml>\n",[149],"html",[80,301,302,313,323,338,347],{"__ignoreMap":92},[96,303,304,307,310],{"class":98,"line":99},[96,305,306],{"class":158},"\u003C",[96,308,299],{"class":309},"s8PnL",[96,311,312],{"class":158},">\n",[96,314,315,318,321],{"class":98,"line":132},[96,316,317],{"class":158},"  \u003C",[96,319,320],{"class":309},"body",[96,322,312],{"class":158},[96,324,326,329,331,334,336],{"class":325,"line":149},[98,199],[96,327,328],{"class":158},"    \u003C",[96,330,276],{"class":309},[96,332,333],{"class":158},">\u003C\u002F",[96,335,276],{"class":309},[96,337,312],{"class":158},[96,339,340,343,345],{"class":98,"line":155},[96,341,342],{"class":158},"  \u003C\u002F",[96,344,320],{"class":309},[96,346,312],{"class":158},[96,348,349,352,354],{"class":98,"line":187},[96,350,351],{"class":158},"\u003C\u002F",[96,353,299],{"class":309},[96,355,312],{"class":158},[59,357,358,359,366],{},"Check out the docs for ",[360,361,365],"a",{"href":362,"rel":363},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fextras\u002Fweb-components.html",[364],"nofollow","more details"," on how this works.",[368,369,370],"style",{},"html pre.shiki code .sRZEA, html code.shiki .sRZEA{--shiki-light:#D73A49;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html pre.shiki code .sygvE, html code.shiki .sygvE{--shiki-light:#24292E;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .srga6, html code.shiki .srga6{--shiki-light:#24292E;--shiki-default:#ADD7FF;--shiki-dark:#ADD7FF}html pre.shiki code .sOygg, html code.shiki .sOygg{--shiki-light:#D73A49;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .srEIG, html code.shiki .srEIG{--shiki-light:#032F62;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .skTdI, html code.shiki .skTdI{--shiki-light:#032F62;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html pre.shiki code .sMzB8, html code.shiki .sMzB8{--shiki-light:#24292E;--shiki-default:#E4F0FB;--shiki-dark:#E4F0FB}html pre.shiki code .sHQ3f, html code.shiki .sHQ3f{--shiki-light:#6F42C1;--shiki-default:#E4F0FBD0;--shiki-dark:#E4F0FBD0}html pre.shiki code .s1MN7, html code.shiki .s1MN7{--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-default:#767C9DB0;--shiki-default-font-style:italic;--shiki-dark:#767C9DB0;--shiki-dark-font-style:italic}html pre.shiki code .slWGy, html code.shiki .slWGy{--shiki-light:#D73A49;--shiki-default:#91B4D5;--shiki-dark:#91B4D5}html pre.shiki code .sGRIV, html code.shiki .sGRIV{--shiki-light:#005CC5;--shiki-default:#E4F0FB;--shiki-dark:#E4F0FB}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s8PnL, html code.shiki .s8PnL{--shiki-light:#22863A;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}",{"title":92,"searchDepth":132,"depth":132,"links":372},[373,374,375],{"id":74,"depth":149,"text":75},{"id":242,"depth":149,"text":243},{"id":289,"depth":149,"text":290},[377,378],"template","script","vue-tips\u002Fdefining-and-registering-vue-web-components-static-v1.jpg","2022-11-04","md",null,{},"\u002Fvue-tips\u002Fdefining-and-registering-vue-web-components",{"text":386,"minutes":387,"time":388,"words":389},"2 min read",1.135,68100,227,{"title":54,"description":61},{"loc":384,"lastmod":380},"vue-tips\u002Fdefining-and-registering-vue-web-components\u002Findex","3TPKr40jsGYW__R7CST2zFSSNQODJJQ1zNCADWL7hEw",{"id":395,"title":54,"body":396,"cover":379,"description":61,"extension":381,"meta":610,"navigation":11,"path":384,"robots":382,"seo":613,"sitemap":614,"stem":392,"__hash__":615},"content\u002Fvue-tips\u002Fdefining-and-registering-vue-web-components\u002Findex.md",{"type":56,"value":397,"toc":605},[398,400,404,406,408,412,498,504,510,512,518,544,546,548,598,603],[59,399,61],{},[63,401,402],{},[59,403,67],{},[59,405,70],{},[72,407,75],{"id":74},[59,409,78,410,83],{},[80,411,82],{},[85,413,415],{"className":87,"code":88,"highlights":414,"language":91,"meta":92,"style":92},[90],[80,416,417,435,449,453,473,477,481],{"__ignoreMap":92},[96,418,419,421,423,425,427,429,431,433],{"class":98,"line":99},[96,420,103],{"class":102},[96,422,107],{"class":106},[96,424,111],{"class":110},[96,426,114],{"class":106},[96,428,118],{"class":117},[96,430,122],{"class":121},[96,432,126],{"class":125},[96,434,129],{"class":121},[96,436,437,439,441,443,445,447],{"class":98,"line":132},[96,438,103],{"class":102},[96,440,137],{"class":110},[96,442,118],{"class":117},[96,444,122],{"class":121},[96,446,144],{"class":125},[96,448,129],{"class":121},[96,450,451],{"class":98,"line":149},[96,452,152],{"emptyLinePlaceholder":11},[96,454,455,457,459,461,463,465,467,469,471],{"class":98,"line":155},[96,456,159],{"class":158},[96,458,162],{"class":106},[96,460,166],{"class":165},[96,462,169],{"class":106},[96,464,172],{"class":158},[96,466,162],{"class":106},[96,468,177],{"class":158},[96,470,180],{"class":106},[96,472,184],{"class":183},[96,474,475],{"class":98,"line":187},[96,476,152],{"emptyLinePlaceholder":11},[96,478,479],{"class":98,"line":192},[96,480,195],{"class":183},[96,482,484,486,488,490,492,494,496],{"class":483,"line":90},[98,199],[96,485,203],{"class":202},[96,487,207],{"class":206},[96,489,210],{"class":202},[96,491,111],{"class":165},[96,493,169],{"class":106},[96,495,172],{"class":158},[96,497,219],{"class":106},[59,499,222,500,226,502,230],{},[80,501,225],{},[80,503,229],{},[59,505,233,506,236,508,239],{},[80,507,229],{},[80,509,229],{},[72,511,243],{"id":242},[59,513,246,514,250,516,254],{},[80,515,249],{},[80,517,253],{},[85,519,520],{"className":87,"code":257,"language":91,"meta":92,"style":92},[80,521,522],{"__ignoreMap":92},[96,523,524,526,528,530,532,534,536,538,540,542],{"class":98,"line":99},[96,525,264],{"class":158},[96,527,162],{"class":106},[96,529,249],{"class":165},[96,531,169],{"class":106},[96,533,273],{"class":121},[96,535,276],{"class":125},[96,537,273],{"class":121},[96,539,281],{"class":106},[96,541,284],{"class":158},[96,543,219],{"class":106},[72,545,290],{"id":289},[59,547,293],{},[85,549,551],{"className":296,"code":297,"highlights":550,"language":299,"meta":92,"style":92},[149],[80,552,553,561,569,582,590],{"__ignoreMap":92},[96,554,555,557,559],{"class":98,"line":99},[96,556,306],{"class":158},[96,558,299],{"class":309},[96,560,312],{"class":158},[96,562,563,565,567],{"class":98,"line":132},[96,564,317],{"class":158},[96,566,320],{"class":309},[96,568,312],{"class":158},[96,570,572,574,576,578,580],{"class":571,"line":149},[98,199],[96,573,328],{"class":158},[96,575,276],{"class":309},[96,577,333],{"class":158},[96,579,276],{"class":309},[96,581,312],{"class":158},[96,583,584,586,588],{"class":98,"line":155},[96,585,342],{"class":158},[96,587,320],{"class":309},[96,589,312],{"class":158},[96,591,592,594,596],{"class":98,"line":187},[96,593,351],{"class":158},[96,595,299],{"class":309},[96,597,312],{"class":158},[59,599,358,600,366],{},[360,601,365],{"href":362,"rel":602},[364],[368,604,370],{},{"title":92,"searchDepth":132,"depth":132,"links":606},[607,608,609],{"id":74,"depth":149,"text":75},{"id":242,"depth":149,"text":243},{"id":289,"depth":149,"text":290},{"categories":611,"date":380,"readingTime":612},[377,378],{"text":386,"minutes":387,"time":388,"words":389},{"title":54,"description":61},{"loc":384,"lastmod":380},"UnXZvUgMRr_54PxNEAzWeAN0G8VOea8Y40iVIAaEOHA",[617,621],{"title":618,"path":619,"stem":620,"children":-1},"Vue Tip: Use Vue Without Build Step","\u002Fvue-tips\u002Fuse-vue-without-build-step","vue-tips\u002Fuse-vue-without-build-step\u002Findex",{"title":622,"path":623,"stem":624,"children":-1},"Vue Tip: Share Composable State Across Components","\u002Fvue-tips\u002Fshare-composable-state-across-components","vue-tips\u002Fshare-composable-state-across-components\u002Findex"]