从本地vite导入的组件不更新
我正在研究一个新项目的选择。我们正在考虑使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
今天我的软件包遇到了这个问题,最后,我发现真正的罪魁祸首是我们本地测试的软件包内的
node_module
。如果您通过npm链接安装本地软件包
或直接使用文件夹路径安装,例如“组件”:“ ../组件”
,您的node_module
将看起来像:您的软件包将使用其自己的
node_module
在该模块内部发货。因此,您的组件无法正常工作。要测试它,只需删除
node_modules/components/node_modules
,而Vite Cachenode_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 bynpm link
or install directly with the folder path like"components": "../components"
, yournode_module
will look like:Your package will be shipped with its own
node_module
and inside that module has avue
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 cachenode_modules/.vite
then runyarn dev
again. You will see your component works fine now.Solution:
components
runnpm pack
to pack your package. It will create a tarball for your package. The output is thecomponents-0.0.0.tgz
file inside thecomponents
folder. This is the most important part becausenpm pack
will create a pack of your package that is similar to what you will publish to the npm registry.my-vite-app
add your package to thepackage.json
:"components": "file:../components/components-0.0.0.tgz"
yarn
to install the package andyarn dev
to run the app and see if your components work.在您的
组件中
文件夹运行:然后运行:
in
my-vite-app
文件夹运行:在
naver.ts.ts
中导入样式:In your
components
folder run :then run :
in
my-vite-app
folder run :in the
maint.ts
import the style :