我正在开发VUE中的一个Web组件.其中很多都是使用vutify组件库制作的,我目前正在try 将其导出到Web组件.我遵循了这个线程中描述的步骤:How can Add libraries like Vuetify inside of a web component created by Vue 3?,它包括覆盖标准的‘fineCustomElement’方法,但是在完成了所有步骤之后,我现在无法将props 传递给index.html文件中的导出组件.

一些代码片段给你一个 idea :

  1. Very Simplified version of my component using vuetify

  2. 将验证文件绑定到Web组件(如上面的线程中所述) 我还在方法中分别声明了props ,因为否则我会得到一个错误,后面跟着一个白屏.但是,即使在声明之后,它们也不会传递到实际组件上.

Binding vuetify files to the web-component  

  1. 将props 添加到index.html文件中的自定义组件 <custom-vuetify-element checkProp = "foo"></custom-vuetify-element>

这个问题的原因可能是什么?在覆盖定义CustomElement方法中,是否有什么可以更改以使传递props 成为可能?

如有任何帮助,将不胜感激! 提前谢谢你

try 了各种方法.不幸的是,这个具体的问题并没有在其他地方真正被提及.

推荐答案

您可以try 直接使用它,而不是包装传入的组件:

const defineCustomElement = (component, { plugins = [] } = {}) =>
  VueDefineCustomElement({
    ...component,
    setup(...args) {
      const app = createApp({})
      plugins.forEach(app.use)
      const instance = getCurrentInstance()
      Object.assign(instance.appContext, app._context)
      Object.assign(instance.provides, app._context.provides)

      return component.setup?.(...args)
    },
  })

playground


在被覆盖的defineCustomElement()函数中包装组件时,需要传递props 和emits ,然后将它们连接到组件实例:

const defineCustomElement = (component, { plugins = [] } = {}) =>
  VueDefineCustomElement({
    props: component.props, // <---- pass on component props
    emits: component.emits, // <---- pass on component emits
    setup(props, {emit}) {
      const app = createApp({})
      plugins.forEach(app.use)
      const instance = getCurrentInstance()
      Object.assign(instance.appContext, app._context)
      Object.assign(instance.provides, app._context.provides)

      const emitProps = turnEventNamesToProps(component.emits, emit) // <--- turn events into props (see below)

      return () => h(component, {...props, ...emitProps}) // <--- return render function from setup
    },
  })

若要将emits 传递给实例,必须将它们转换为props .例如,如果您的组件发出一个名为myEvent的事件,则需要传入一个带有处理程序的属性onMyEvent,该处理程序在包装组件上触发发出.

下面是一个示例,它将事件名称数组转换为以属性名称为键、以处理程序为值的对象:

const emitEventToProp = eventName => 'on' + eventName[0].toUpperCase() + eventName.slice(1)
const turnEventNamesToProps = (eventNames, emitter) => {
  const buildHandler = (eventName) => (...args) => emitter(eventName, ...params)
  const entries = eventNames.map(eventName => [
    emitEventToProp(eventName), 
    buildHandler(eventName)
  ])
  return Object.fromEntries(entries)
}

请注意,Vue在Web组件上可以监听的事件名称似乎受到限制.虽然Web组件可以发出update:modelValue事件,甚至还会发出update:model-value事件,但在Vue模板中将@update:modelValue放在Web组件上似乎不会注册侦听器.

这是一张playground元的

Javascript相关问答推荐

使用Angular 17计算从生日日期起的年、月、天

TestCafe预计文本等于以下内容之一

React Hooks中useState的同步问题

序列查找器功能应用默认值而不是读取响应

如何使用Echart 5.5.0创建箱形图

使用useup时,React-Redux无法找到Redux上下文值

materialized - activeIndex返回-1

通过使用100%间隔时间来代表我们还剩多少时间来倒计时

我应该在redux reducer中调用其他reducer函数吗?

Google Apps脚本中的discord邀请API响应的日期解析问题

通过嵌套模型对象进行Mongoose搜索

网页自检测外部元素无法加载

Redux查询多个数据库Api reducerPath&

在nextjs服务器端api调用中传递认证凭证

在react JS中映射数组对象的嵌套数据

使用js构造一个html<;ath&>元素并不能使其正确呈现

使用auth.js保护API路由的Next.JS,FETCH()不起作用

是否可以将异步调用与useState(UnctionName)一起使用

通过跳过某些元素的对象进行映射

如何根据输入数量正确显示alert ?