<template>
<view class="content">
<image class="logo" src="../../static/logo.png"></image>
<view>
<text class="title">{{title}}</text>
</view>
</view>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
}
});
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin: 200rpx auto 50rpx auto;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
- 发布:2024-07-10 10:10
- 更新:2025-11-06 17:23
- 阅读:431
【报Bug】vue2+ts+nvue 编译到安卓时报错:TS2688:Cannot find type definition file for 'uni-app-vue2'.
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows 10 专业版 22H2
手机系统: Android
手机系统版本号: Android 14
手机厂商: OPPO
手机机型: find x2
页面类型: nvue
vue版本: vue2
打包方式: 云端
项目创建方式: CLI
CLI版本号: v5.0.8
示例代码:
操作步骤:
编译到安卓报错
编译到安卓报错
预期结果:
.
.
实际结果:
.
.
bug描述:
vue2+ts,nvue页面编译报错:
09:51:02.304 项目 'm-ts-project' 编译成功。
09:51:02.324 [tsl] ERROR at ..:0
09:51:02.345 TS2688:Cannot find type definition file for 'uni-app-vue2'.
09:51:02.365 The file is in the program because:
09:51:02.385 Entry point of type library 'uni-app-vue2' specified in compilerOptions
09:51:02.406 ERROR Build failed with errors.
cli工程项目:v5.0.8
uniapp版本:v4.15
虽然不知道是什么原因,但是我找到另外一种解决方案
创建一个fixTsError.js
在package.json里面配置scripts: {"postinstall": "node ./fixTsError"}
// uniapp 项目 nvue webpack4项目 里面使用ts有bug,会去检查各种node_modules错误
// 直接修改 ts-loader 设置忽略所有ts错误,只转义ts代码,解决ts报错问题,ts的各种问题
// 编辑器提示自己解决
const path = require('path')
const fs = require('fs')
const curFile = path.join(__dirname, 'node_modules/@dcloudio/vue-cli-plugin-uni/lib/util.js')
const curJsText = fs.readFileSync(curFile).toString()
const regConfig = /configFile:\s*tsConfigJsonFile/
const match = curJsText.match(regConfig);
if(match){
console.error('查找到ts-loader配置文件')
const regTranspileOnly = /transpileOnly:\s*true/
const transpileMatch = curJsText.match(regTranspileOnly);
if (transpileMatch) {
// 已更新
console.error('已更新ts-loader配置文件')
}else {
const newJsText = curJsText.replace(match[0], `${match[0]},
transpileOnly:true`)
console.error('更新ts-loader配置,添加transpileOnly:true')
fs.writeFileSync(curFile, newJsText)
}
}
少林寺方丈嘿嘿
有找到原因吗
2024-10-22 15:52