从本地vite导入的组件不更新

发布于 2025-02-08 04:16:24 字数 611 浏览 2 评论 0原文

我正在研究一个新项目的选择。我们正在考虑使用NUXT(或只是常规VUE 3),并创建一个库,我们将保留共享组件。

我正在尝试进行初始设置,但是有问题。我关注此教程以创建库和typescript。我创建了一个带有计数器的示例组件并导出了它。

问题在于,当我在消费项目中从库中导入组件(无论是NUXT项目还是Vanilla Vite Vue项目)时,该组件看起来似乎没有反应性。单击时,它的内部计数器应该增加,但没有更新。控制台中没有错误或警告。

另一个问题是,它的CSS没有被应用。它具有组件中定义的一些基本样式,但不可见。我在此处使用设置说明创建了一个最小的复制回购: https://github.com/drekembe/ Vite-Reproduction-2342

我尝试搜索类似问题或自己调试,但我还没有到达任何地方。

任何帮助将不胜感激。

I'm researching options for a new project at work. We're thinking of using nuxt (or just regular vue 3) and creating a library where we will be keeping our shared components.

I'm trying to do the initial setup, but am having problems. I followed this tutorial to create the library and added typescript to it. I created a sample component with a counter and exported it.

The problem is that when I import the component from my library in a consuming project (whether it's the nuxt project or a vanilla vite vue project), the component looks like it's not reactive. Its internal counter is supposed to increase when it's clicked, but it's not updating. There are no errors or warning in the console.

Another issue is that its CSS is not being applied. It has some basic styling defined in the component, but it's not visible. I've created a minimal reproduction repo with setup instructions here: https://github.com/drekembe/vite-reproduction-2342

I've tried searching for similar issues or debugging it myself, but I haven't gotten anywhere.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2025-02-15 04:16:24

今天我的软件包遇到了这个问题,最后,我发现真正的罪魁祸首是我们本地测试的软件包内的node_module。如果您通过npm链接安装本地软件包或直接使用文件夹路径安装,例如“组件”:“ ../组件”,您的node_module将看起来像:

node_modules
  |
   --components
      |
       --node_modules <-- the culprit here

您的软件包将使用其自己的node_module在该模块内部发货。因此,您的组件无法正常工作。

要测试它,只需删除node_modules/components/node_modules,而Vite Cache node_modules/.vite然后再次运行yarn dev。您将看到您的组件现在正常工作。

解决方案:

  • 在您的软件包文件夹中组件运行npm pack打包软件包。它将为软件包创建tarball 。输出是组件-0.0.0.tgz 组件文件夹中的文件。这是最重要的部分,因为npm pack将创建一包与您将发布到NPM注册表相似的软件包。
  • 现在在您的测试项目my-vite-app将您的软件包添加到package.json“组件”:“文件:../组件/components- 0.0.0.tgz“
  • 运行YARN安装软件包和YARN DEV运行该应用程序,看看您的组件是否有效。
  • 每当您对包装进行更改时,请不要忘记再次打包包装并重新安装它。您可能想增加包装版本以使纱线缓存无效

I encounter this problem today with my package and finally, I found the real culprit is the node_module inside the package that we tested locally. If you install the local package by npm link or install directly with the folder path like "components": "../components", your node_module will look like:

node_modules
  |
   --components
      |
       --node_modules <-- the culprit here

Your package will be shipped with its own node_module and inside that module has a vue package that is independent of the vue package that you are using in your app. So your components would not work as expected.

To test it, just delete the node_modules/components/node_modules and the vite cache node_modules/.vite then run yarn dev again. You will see your component works fine now.

Solution:

  • In your package folder components run npm pack to pack your package. It will create a tarball for your package. The output is the components-0.0.0.tgz file inside the components folder. This is the most important part because npm pack will create a pack of your package that is similar to what you will publish to the npm registry.
  • Now in your test project my-vite-app add your package to the package.json: "components": "file:../components/components-0.0.0.tgz"
  • Run yarn to install the package and yarn dev to run the app and see if your components work.
  • Every time you make a change on your package, don't forget to pack the package again and re-install it. You might want to increase your package version to invalidate the yarn cache
時窥 2025-02-15 04:16:24

在您的组件中文件夹运行:

yarn build

然后运行:

yarn link

in my-vite-app文件夹运行:

 yarn link "components"

naver.ts.ts中导入样式:

import { createApp } from 'vue'
import App from './App.vue'
import 'components/dist/style.css'
createApp(App).mount('#app')

In your components folder run :

yarn build

then run :

yarn link

in my-vite-app folder run :

 yarn link "components"

in the maint.ts import the style :

import { createApp } from 'vue'
import App from './App.vue'
import 'components/dist/style.css'
createApp(App).mount('#app')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文