简体   繁体   English

为什么我在使用 vue-sfc-rollup 构建的 Vue 2 组件库中使用 Vue 2 Composition API 时会出错?

[英]Why do I get an error when I use the Vue 2 Composition API in a Vue 2 component library built with vue-sfc-rollup?

I am building a component library using vue-sfc-rollup .我正在使用vue-sfc-rollup构建组件库。 The library targets Vue 2 but uses the Vue 2 Composition API to make it easier to port to Vue 3 when the time comes.该库以 Vue 2 为目标,但使用Vue 2 组合 API以便在时机成熟时更容易移植到 Vue 3。

After publishing the library to npm, I created a blank Vue 2 project using Vue CLI (targeting Vue 2 with TypeScript enabled).将库发布到 npm 后,我使用 Vue CLI 创建了一个空白的 Vue 2 项目(以启用 TypeScript 的 Vue 2 为目标)。

npm install <my_package>
npm install @vue/composition-api

In main.ts I installed the Composition API as a plugin.main.ts ,我将 Composition API 安装为插件。

import Vue from 'vue'
import VueCompositionApi from '@vue/composition-api'

Vue.use(VueCompositionApi)

Then, in the App.vue I imported a component from my library to test.然后,在App.vue中,我从库中导入了一个组件进行测试。

import { MyComponent } from '@mypackage/components'

export default Vue.extend({
  components: {
    MyComponent,
  }
})

However, when I run this in the browser, I get the following error.但是,当我在浏览器中运行它时,出现以下错误。

Error in data(): "TypeError: Cannot read property 'config' of null" data() 中的错误:“TypeError:无法读取 null 的属性 'config'”

Removing Vue.use(VueCompositionApi) from main.ts gets rid of the error but then the component doesn't load.main.ts中删除Vue.use(VueCompositionApi)可以消除错误,但不会加载组件。 So it seems the issue is my component isn't properly registering that the Composition API exists.所以看来问题是我的组件没有正确注册组合 API 的存在。

The issue was that rollup needs to be told that the library has an external dependency.问题是需要告诉汇总库具有外部依赖项。

In the build/rollup.config.js search for const external and add @vue/composition-api to the list.build/rollup.config.js搜索const external并将@vue/composition-api添加到列表中。

const external = [
  'vue',
  '@vue/composition-api',  // <-- add this!
]

Then just build, publish to npm, npm update the package in the project consuming the package and it should work without error.然后只需构建,发布到 npm,npm 更新使用包的项目中的包,它应该可以正常工作。

Thanks to this post on Github that steered me to the external config and this example rollup.config.js the post links to that showed exactly where it should go.感谢Github 上的这篇文章,它引导我使用external配置和这个示例rollup.config.js ,指向该文章的链接准确地显示了它应该去的地方。

暂无
暂无

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

相关问题 将 Tailwind CSS 与 vue-sfc-rollup 一起使用 - Using Tailwind CSS with vue-sfc-rollup 从使用 vue-sfc-rollup 生成的库中导入组件的问题 - issues with importing components from a library generated using vue-sfc-rollup 我是否必须在 Vue 3 中使用组合 API,或者我仍然可以按照“Vue 2”的方式做事吗? - Do I have to use the Composition API in Vue 3, or can I still do things the "Vue 2" way? 我可以在 SFC 中定义一个 Vue JSX 组件吗<script setup> and use it in the SFC template? - Can I define a Vue JSX component within a SFC <script setup> and use it in the SFC template? rollup:如何在 Vue sfc 中汇总顺风 scss? - rollup: how to rollup tailwind scss in Vue sfc? 如何在Vue2中使用vue class组件与api by typescript - How to use vue class component with composition api in Vue2 by typescript Vue 3 vue-国际化。 如何在应用程序(.vue 文件)之外使用 $t(t)? 作文 API - Vue 3 vue-i18n. How to use $t(t) outside the app(.vue files)? Composition API 在使用 Vue 组合 API 和 TypeScript 时声明作为单个文件组件的模板引用时应该使用什么类型? - What type should I use when declaring a template ref that is a single file component when using the Vue Composition API and TypeScript? 是否可以将 SFC Vue 组件用作 mixin? - Is it possible to use a SFC Vue component as a mixin? 何时使用 Vue Composition 的 setup() 钩子 API - When to use setup() hook of Vue Composition API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM