Last active
          March 2, 2024 16:35 
        
      - 
      
- 
        Save jaalorsa517/e2e167bd39f2b7c36cfb12ed895b28b9 to your computer and use it in GitHub Desktop. 
    Script base para incorporar dentro del proyecto Vue, para hacer un Custom Element
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import { defineCustomElement, h, createApp, getCurrentInstance } from "vue"; | |
| export const vueDefineCustomElement = (component, { plugins = [] } = {}) => | |
| defineCustomElement({ | |
| ...component, | |
| setup(props, { emit }) { | |
| const app = createApp(component); | |
| // install plugins | |
| plugins.forEach(app.use); | |
| const inst = getCurrentInstance(); | |
| Object.assign(inst.appContext, app._context); | |
| Object.assign(inst.provides, app._context.provides); | |
| //translate events | |
| const toCamelCase = (str) => str.slice(0, 1).toUpperCase() + str.slice(1); | |
| let events = {}; | |
| if (inst.emitsOptions) { | |
| events = Object.keys(inst.emitsOptions).reduce((acc, key) => { | |
| acc[`on${toCamelCase(key)}`] = (...args) => emit(key, ...args); | |
| return acc; | |
| }, {}); | |
| } | |
| return () => | |
| h(component, { | |
| ...props, | |
| ...events, | |
| }); | |
| }, | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment