1、vue 文件中的 script 标签加了 lang="ts" 后无法成功引入
[typescript] ts-loader! Module build failed: Error: Could not find source file:
- 原因:主要是由于
vue文件首先被vue-loader识别,但vue-loader内部并没有对内部的ts进行处理,所以无法识别script中的代码。 - 解决:只需要在
vue-loader的options中加入一个ts-loader就可以了。
{
test: /\.vue$/,
loader: "vue-loader",
options: {
loaders: {
less: [
"vue-style-loader",
"css-loader",
"less-loader?indentedSyntax",
],
ts: [
{
loader: "ts-loader",
options: {
appendTsSuffixTo: [/\.vue$/],
},
},
],
},
},
}
2、ts 文件中无法识别 vue 文件和 window 自定义属性
- 原因:由于
TypeScript默认并不支持*.vue后缀的文件,所以在vue项目中引入的时候需要创建一个vue-shim.d.ts文件,放在项目对应使用目录下(也可在tsconfig.json中的include配置扫描路径),例如src/vue-shim.d.ts。 - 解决:
// 用于支持 ts 引入 vue文件
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
// 用于支持 this.$router 这种形式的调用
declare module "vue/types/vue" {
interface Vue {
$myProperty: string;
}
}
// Window 自定义属性,当然也可以直接 (window as any).ethereum 这样使用
declare global {
interface Window {
isSupportWebp: boolean
web3: any
ethereum: any
}
}
// 文件尾部导出一个空对象,保证定义的类型生效
export {}
注意:
注意即使此声明文件不需要导出任何东西,也需要导出一个空对象(或者文件中包含 import 语句也可以),用来告诉编译器这是一个模块的声明文件,否则类型定义无效。
3、错误 “SyntaxError: Unexpected token } in JSON at position”
- 原因:主要是因为
json文件中的格式有问题,导致后面使用JSON.parse解析时报错。 - 解决:复制
json内容到FE helper中检查并修改后再试试。
4、使用 postcss 时样式中的注释报错
- 原因:主要是因为
loader加载顺序不对导致的(从右往左解析)。 - 解决:比如说使用的是
less,只需要调整loader的顺序为"vue-style-loader","css-loader", "postcss-loader"
即可。
5、this.$refs 无法识别
-
原因:
typescript无法识别出this.$refs.xx是否含有xx方法。 -
解决:方法比较多:
- 一劳永逸,直接声明为 any
const myRef: any = this.$refs.myModal;- 类型断言
(this.$refs.myModal as Vue & {resetFields:Function}).resetFields();- 接口继承
- 直接引入
vue-class-component,更直观的定义$refs的类型
<template> <input ref="input" /> </template><script lang="ts"> import Vue from "vue"; import Component from "vue-class-component"; @Component export default class InputFocus extends Vue { // annotate refs type. // The symbol `!` (definite assignment assertion) // is needed to get rid of compilation error. $refs!: { input: HTMLInputElement; }; mounted() { // Use `input` ref without type cast. this.$refs.input.focus(); } } </script>
6、vue-class-component 和 vue-property-decorator 支持
- vue-class-component 对
Vue组件进行了一层封装,让Vue组件语法在结合了TypeScript语法之后更加扁平化,vue-property-decorator 是在vue-class-component上增强了更多的结合Vue特性的装饰器。 - 这 2 个包可以让我们更优雅的书写代码。
npm i -D vue-class-component vue-property-decorator
7、运行测试用例时 vue-jest 报错
Babel bug: .inputSourceMap must be a boolean, object, or undefined & filename is undefined
- 原因:由于当前安装的
babel和引用的babel-core版本不匹配导致的,babel6对应babel-core,babel7对应@/babel-core。 - 解决:通过搜索和查阅文档,发现已在指定版本修复,安装对应版本即可修复。
8、html-webpack-plugin 配置 template 无效
一直使用根目录下的
index.html,且其他配置也无效
- 原因:主要是由于
webpack配置中的publicPath配置的问题,使用时需要格外注意。 - 解决:通过判断
process.env.NODE_ENV分别设置开发和生产环境的资源路径。
output: {
path: pathResolve("dist"),
publicPath: process.env.NODE_ENV === "production" ? "." : "/",
filename: "build.js?[hash:5]",
}
9、npm 包版本号导致的各种报错
- 原因:由于不同的
npm包适配的环境不一样,使用时需要格外注意。 - 解决:通过搜索和查阅文档,找到可以正常工作的匹配版本,尽量避免过高或过低的版本。
10、TS 中 any 和 unknown 的区别?
unknown是 top type ,任何类型都是它的subtype,也就是说它可以被赋为任何值,但只能赋值给any和unknow类型的变量。any是 top type + bottom type ,任何类型都是它的subtype,它也是任何类型的subtype,也就是说any基本上就是关闭了类型检查。
11、Plugin 与 Preset 执行顺序
preset相当于包含一个或多个plugin,可以避免手动安装多个plugin,提高效率,减少出错几率,比如@babel/preset-env。
可以同时使用多个Plugin和Preset,此时它们的执行顺序非常重要。
- 先执行完所有 Plugin,再执行 Preset
- 多个 Plugin,按照声明次序顺序执行
- 多个 Preset,按照声明次序逆序执行
12、Jest did not exit one second after the test run has completed
- 原因:使用
jest执行完测试用例后,提示jest没有自动退出,猜想应该是有异步任务没结束导致的。 - 解决:改写相关代码,让任务能终止
import { throttle } from "./throttle";
import { timer } from "rxjs";
describe("testing", () => {
it("test-throttle", (done) => {
let count = 0;
function __addCount() {
count += 1;
}
const addCount = throttle(__addCount, 100);
let source = timer(0, 10).subscribe((i: number) => {
if (i === 100) {
expect(count).toBe(12);
done();
source.unsubscribe(); // 这里及时停止计时器即可
} else {
addCount();
}
});
});
});
13、mac 下提示 permission denied
-
原因:没有执行权限
-
解决:
chmod是权限管理命令change the permissions mode of a file的缩写。u代表所有者,x代表执行权限,+表示增加权限。chmod u+x file.sh就表示对当前目录下的file.sh文件的所有者增加可执行权限。
14、解决 typescript Cannot redeclare block-scoped variable
- 原因:没有模块化,会有变量污染的问题
- 解决:只需要在文件末尾加上
export {}
15、Could not find a declaration file for module ‘xxx’. ‘xxx’ implicitly has an 'any’type.
- 场景:
TS项目中,通过import引入js文件提示找不到module - 原因:没有对
js定义模块 - 解决:只需要在
shim.d.ts中添加一行declare module "\*.js";


评论区