打包后template中this为undefined
Vue version
^3.2.37
Link to minimal reproduction
https://github.com/qilimazhualuo/template_this_undefined
Steps to reproduce
默认新增项目
新增文件
- src/constant.js
报错方式 然后再main.js中引用
app.vue中template 通过this引用该变量
开发服务中正常显示 但是打包后报错 Cannot read properties of undefined (reading '_ymdhms') ( _ymdhms 是我的全局变量 ) 打包文件已上传
What is expected?
开发服务与打包后行为一致(全部正常显示或全部undefined)
What is actually happening?
项目打包后在template中使用的this报错undefined 开发服务: 打包后:
System Info
D:\我的库\template_this_undefined>npx envinfo --system --npmPackages vue --binaries --browsers
Need to install the following packages:
envinfo
Ok to proceed? (y) y
System:
OS: Windows 10 10.0.22000
CPU: (6) x64 AMD Ryzen 5 4500U with Radeon Graphics
Memory: 3.62 GB / 15.37 GB
Binaries:
Node: 16.14.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 8.3.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.22000.120.0), Chromium (104.0.1293.63)
Internet Explorer: 11.0.22000.120
npmPackages:
vue: ^3.2.37 => 3.2.37
Any additional comments?
No response
This is not required in template. You can compile constant.js in SFC Playground.
In template, variables are automatically get from ctx.