繁体   English   中英

Vue3 onMounted function 在与汇总捆绑在一起的 esm package 中不起作用

[英]Vue3 onMounted function doesn’t work in esm package bundled with rollup

我将 vue3 组件与汇总捆绑在一起。 组件很简单:

// main.vue
<template>
  <div>
    <div @click="clickDiv">{{ 1 }}</div>
    <div ref="divRef"></div>
  </div>
</template>
<script>
import { onMounted, ref, defineComponent } from 'vue'
export default defineComponent({
  name: 'Vue3Component',
  mounted () {
    console.log('component mounted') // it works well
  },
  setup() {
    const divRef = ref()
    const clickDiv = () => {
      console.log('divRef', divRef) // it causes some warnings
    }
    onMounted(() => {
      console.log('component onMounted') // it causes some warnings
    })
    return {
      a: ref(1),
      clickDiv,
      divRef
    }
  },
})
</script>

和输入条目:

// index.js
import Vue3Component from './main.vue'
Vue3Component.install = (app) => {
  app.component(Vue3Component.name, Vue3Component)
}

export {
  Vue3Component
}

汇总配置代码:

// rollup.config.js
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import pkg from './package.json'
import external from 'rollup-plugin-peer-deps-external'
import vuePlugin from 'rollup-plugin-vue'

const extensions = ['.vue', '.js']

const globals = {
  vue: 'Vue',
  'highlight.js': 'hljs',
  'marked': 'marked'
}

export default [
  {
    input: 'src/index.js',
    output: [
      {
        name: 'VueNextMEditor',
        file: pkg.main,
        format: 'umd',
        globals
      },
      {
        file: pkg.module,
        format: 'es'
      },
      {
        name: 'VueNextMEditor',
        file: pkg.unpkg,
        format: 'umd',
        plugins: [terser()],
        globals
      }
    ],
    plugins: [
      external(),
      vuePlugin(),
      resolve(),
      commonjs({ extensions })
    ]
  }
]

当我使用 vue-cli 在本地测试组件时,设置中的onMounted不起作用。 浏览器显示如下错误:

[Vue warn]: onMounted is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
[Vue warn]: Missing ref owner context. ref cannot be used on hoisted vnodes. A vnode with ref must be created inside the render function. 
  at <Vue3Component> 
  at <App>

mounted在 function 工程之外的工作会。 任何人都可以帮我解决这个问题吗? Vue3组件

我解决了这个问题。 发生这种情况是因为我安装了两次vue 我用pnpm link解决了

$ cd vue3-component
$ pnpm install

$ cd example
$ pnpm link ../node_modules/vue
$ pnpm install
$ pnpm serve

有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM