Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序下import的编译问题 #4962

Open
ziveen opened this issue Jun 5, 2024 · 3 comments
Open

小程序下import的编译问题 #4962

ziveen opened this issue Jun 5, 2024 · 3 comments
Labels
mp-weixin 微信小程序 vue3 等待用户反馈 waiting feedback

Comments

@ziveen
Copy link

ziveen commented Jun 5, 2024

环境:

  • vue:3.2.45
  • uni: 3.0.0-alpha-3080220230511001
  • vite: 4.0.3

问题描述:
运行命令yarn dev:mp-weixin后,控制台会报错Charts.Charts is not a constructor.代码片段如下

相关代码

*** lib.js***

function Charts() {
 // do something
}

Charts.prototype.init = function() {}

module.exports = Charts;

pages/index.vue

<template>
     <view></view>
</template>
<script>
import Charts from './lib'
onLoad() {
  const charts = new Charts();
}
</script>

问题处理:

修改lib.js中的导出方式为export default Charts,问题就能正确处理。

分析

这个应该属于uni模块中vite的编译问题,我们项目上一个版本中使用HBUilderx编译无问题。

@Otto-J Otto-J added vue3 mp-weixin 微信小程序 labels Jun 6, 2024
@Otto-J
Copy link
Member

Otto-J commented Jun 6, 2024

你的意思是自定义 js,你使用 cjs 的导出,功能有问题,改为 esm 导出是正常的对吗?你的上一个版本是那个版本,也是 alpha 吗

@Otto-J Otto-J added the 等待用户反馈 waiting feedback label Jun 6, 2024
@ziveen
Copy link
Author

ziveen commented Jun 11, 2024

上一个版本是指的使用Hbuilder运行的项目,现在的使用cli。

@julytian
Copy link

上一个版本是指的使用Hbuilder运行的项目,现在的使用cli。

试下使用@rollup/plugin-commonjs插件配合是否解决

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mp-weixin 微信小程序 vue3 等待用户反馈 waiting feedback
Projects
None yet
Development

No branches or pull requests

3 participants