tsconfig之include和exclude详解

include 作用

指定需要编译处理的文件列表,支持 glob 模式匹配,文件的解析路径相对于当前项目的 tsconfig.json 文件位置

匹配规则

  • *:匹配零个或者多个字符(包括目录的分隔符)
  • ?:匹配任一字符(包括目录分隔符)
  • **/:匹配任何层级的嵌套目录

如果没有明确指定文件扩展名。那么默认情况下仅会包含 .ts.tsx.d.ts 类型的文件。但是如果 allowJs 选项被设置成了 true,那么 .js.jsx 文件也会被包含其中

示例

当我们的目录结构如下

  1. include 配置为 src 或者 src/ 时,src 下的所有文件都会经过 ts 的编译
  2. include 配置为 src/* 时,src 下第一层级的子文件会经过 ts 的编译
  3. include 配置为 src/*.js 时,src 下第一层级的子文件以 .js 结尾的会经过 ts 的编译
  4. include 配置为 src/**/* 时,src 下的所有文件都会经过 ts 的编译,目录层级不限
  5. include 配置为 src/**/*.js 时,src 下以 .js 结尾的会经过 ts 的编译,并且在其对应的目录下面,目录层级不限
  6. include 配置为 **/* 时,包含当前项目中所有文件

exclude 作用

用于指定当解析 include 选项时,需要忽略的文件列表

  • exclude 的默认值是 ["node_modules", "bower_components", "jspm_packages"] 加上 outDir 选项指定的值
  • exclude 配置为 ["src/*.ts"] 代表忽略当前 src 下第一层级的 ts 文件
  • 任何被 filesinclude 指定的文件所引用的文件也会被包含进来,A.ts 引用了 B.ts,因此 B.ts 不能被排除,除非引用它的 A.tsexclude 列表中。

files 作用

用来指定需要编译的文件列表(注意,只能是文件,不能是文件夹)

  • 如果其中任意一个文件无法找到,都会抛出错误
  • 通过 files 属性明确指定的文件却总是会被包含在内,不管 exclude 如何设置
  • 这个配置项适用于你想要指定的文件数量比较少,并且不需要使用 glob 模式匹配的情况。否则,请使用 include 配置项
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Vue 3 中,`include` 和 `exclude` 属性用于配置 `<keep-alive>` 组件的缓存策略,可以接受多个值。你可以通过数组的形式将多个组件名称传递给这两个属性。 以下是一个示例,展示了如何设置 `include` 和 `exclude` 属性的多个值: ```vue <template> <div> <h2>Home Component</h2> <button @click="toggleShowChild">Toggle Show Child</button> <keep-alive :include="includeComponents" :exclude="excludeComponents"> <component :is="currentComponent" v-if="showChild" /> </keep-alive> </div> </template> <script> import ChildComponent1 from './ChildComponent1.vue'; import ChildComponent2 from './ChildComponent2.vue'; import ChildComponent3 from './ChildComponent3.vue'; export default { components: { ChildComponent1, ChildComponent2, ChildComponent3 }, data() { return { showChild: false, currentComponent: 'ChildComponent1', includeComponents: ['ChildComponent1', 'ChildComponent2'], excludeComponents: ['ChildComponent3'] }; }, methods: { toggleShowChild() { this.showChild = !this.showChild; } } }; </script> ``` 在上述示例中,我们使用了 `includeComponents` 和 `excludeComponents` 数组来设置 `include` 和 `exclude` 属性的多个值。 `includeComponents` 数组包含了需要被缓存的组件名称,即只有这些组件会被 `<keep-alive>` 缓存。 `excludeComponents` 数组包含了不需要被缓存的组件名称,即这些组件不会被 `<keep-alive>` 缓存。 注意,在示例中,我们使用了动态组件 `<component>` 来根据 `currentComponent` 的值渲染相应的组件,并将其包裹在 `<keep-alive>` 组件中。 希望以上解答对你有所帮助。如有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wflynn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值