ts-jest 相关问题


Jest + TypeScript + Babel 未知选项:.0

我使用 Jest 为我的一些 Fauna DB 操作编写了一个集成测试。我尝试测试的源代码 (fauna.ts) 和测试本身 (fauna.test.ts) 都是 TypeScript 文件。我已经


玩笑更新后无法读取未定义的属性“html”

对于使用 jest 的 Angular v12 项目,我刚刚将 jest 更新到版本 28。但是现在,我收到以下错误 失败 src/app/components/update-input/update-input.directive.spec.ts ● 测试套件


如何使用 Jest 模拟记忆化的 React 组件

我想模拟一个已被记忆的React组件。我正在使用 React 18.2 和 Jest 29.7 以及 Typescript,以防万一。我已经尝试了几种方法,但似乎找不到有效的方法......


如何使用`@ts-ignore`作为块?

// @ts-ignore 注释使 TypeScript 编译器能够忽略其下面的行。 如何使用 TypeScript 忽略一整段代码?


如何使用Jest函数的动态替换?

我有一系列这样的测试用例: it('应该禁用渲染按钮', async () => { 使成为(...); const 按钮 = screen.getByRole("按钮"); 期待(按钮).toBeDi...


当我尝试在 Visual Code Studio 上运行“ts-node index.ts”等代码时,它显示错误消息

/usr/local/lib/node_modules/ts-node/src/index.ts:857 const DiagnosticText = formatDiagnostics(诊断,diagnosticsHost); 类型错误:formatDiagnostics 不是函数 在 createTSError (/usr/loc...


单元测试因 .getContext() 未实现而引发错误

我正在使用 Jest 为使用画布元素的组件编写测试。当我运行如下所示的测试时,我不断收到错误。 错误:未实现:HTMLCanvasElement.prototype.getCont...


如何在 React 中使用带有 className 属性的 TailwindCSS 实用程序类

我为 React 创建了一个模板(样板)存储库,它提供 ESLint、Typescript、TailwindCSS 和 Jest 测试,并使用 Webpack 构建。 然而,TailwindCSS 实用程序类不...


期望变量为空或布尔值

我正在使用 Jest 为 Node.js 后端开发一些测试,我需要检查来自第三方的一些值。在某些情况下,这些值可以是布尔值或空值。 现在...


配置 Jest 时的配置问题 - TypeScript

可能我有一个配置问题,我只是不知道具体在哪里。错误消息是当我运行测试时: 类型错误:无法读取未定义的属性(读取“品牌”) 14 | 14 ...


如何在 Jest 中模拟只读属性?

我有一个函数,可以将大写的字符串转换为大写前带有破折号的字符串,并使字符串小写。 格式化类型() { // 在大写字母前添加破折号...


笑话未定义

我目前正在使用打字稿制作一个反应应用程序,并且我正在使用反应测试库进行测试。应用程序因 ReferenceError: jest is not Defined 崩溃。不过我所有的测试都通过了。 之前,


[NullInjectorError]:R3InjectorError(独立[_AppComponent])[]:NullInjectorError:没有_HttpClient的提供者

app.module.ts 应用程序模块.ts 应用程序模块.ts 错误 包.json 我的代码已迁移到角度独立 我不知道我做错了什么。我是否必须在其他地方导入或提供 HttpClient...


如何在 React 组件的 Jest 测试中模拟 MUI5 Pro 许可证密钥

我目前正在将 MUI v5 Pro 组件集成到我的 React 库中,然后将这些库组件导入到我的主机应用程序中。我已经在两个 Libra 的 .env 文件中设置了 MUI 许可证密钥...


如何创建松果客户端,它给出错误

存在于松果.ts 中 从 '@pinecone-database/pinecone' 导入 { PineconeClient } 导出 const getPineconeClient = async () => { const client = new PineconeClient() 等待客户。


自动完成功能不适用于 VSCode 中的 TypeScript + Node

所以如果我编写纯 JS (.js),它的工作方式如下: 但对于 TypeScript (.ts) 它不起作用:


在 knex 查询中的 ts 泛型中显示错误 [symbol.iterator()]

const [用户]: T = wait db("users").select("*").returning("*"); // 错误:类型“T”必须具有返回迭代器的“[Symbol.iterator]()”方法。 我的膝盖查询


Cumsum 重置为 NaN

如果我有一个名为 ts 的 pandas.core.series.Series ,其中 1 或 NaN 如下: 第3382章 第3381章 ... 第3369章 第3368章 ... 15 1 10 南 11 1 12 1 13 1 9 南 8 南 ...


vscode 上的 typescript 配置

当我使用 vscode 时收到此消息: 路径 ... ode_modules ypescript\lib 未指向有效的 tsserver 安装。 我不知道出了什么问题。有任何想法吗? ts配置: { ”


无法在 Typescript 中导出常量

有人可以帮助我吗 我有 2 个文件 main.ts 和 hi.ts 命中: 导出常量你好=“dd”; 主要.ts: 从“./hi”导入{hello}; ... A类{ 公共说你好(){ 控制台.log(“...


router.navigate 在异步函数中不起作用

router.navigate 不起作用,页面未重定向到查看器页面。但是如果我在最后一行添加断点,就可以了。 TS: 异步 onApprove(): Promise { if (this.status === true...


运行 Prettier 时出现问题:退出代码 2 和意外标记“:”

每次我尝试提交或运行yarn format(使用此脚本:“format”:“prettier --config \”packages/**/*。{ts,tsx} \“”)或运行make fmt,我收到以下错误


输入非假,又名“真”

在TypeScript中,有truthy的类型吗? 我有这个方法: Object.keys(lck.lockholders).length; 入队(k:任何,obj?:任何):无效 我认为使用 TS 有一种方法可以检查空字符串'...


相当于 @typescript-eslint 中的 noUnusedParameters

我正在努力改善React+TS项目的开发者体验。我发现 noUnusedLocals 和 noUnusedParameters 对于该项目非常有用,但这些规则使开发变得复杂,尤其是......


无法访问项目中的tsconfig路径

我正在尝试在 ts 文件中包含路径,尝试了 stackoverflow 上可能提供的解决方案,但没有一个对我有用 tsconfig.json { “编译器选项”:{ “严格”:真实, ”


为什么 @typescript-eslint/no-redeclare 规则指示误报?

我有一个 typescript 项目,其中通过 eslint 和 typescript-eslint 支持 linting。 请注意,这只是一个最小的工作示例。 这是一个repl链接 索引.ts 函数 testfn(值:...


TS2740:(TS) 类型“VNode”缺少类型“HTMLElement”中的以下属性:accessKey、accessKeyLabel、autocapitalize、dir 以及其他 283 个属性

`类型“VNode”缺少类型“HTMLElement”中的以下属性:accessKey、accessKeyLabel、autocapitalize、dir 以及其他 283 个属性。 > 导入 { init, classModule, propsModule, styleModule,


Angular 中的组件可重用问题

我是角度可重用组件的新手,不确定构建后如何重用该组件。我有 3 个组件父组件、容器组件和可重用组件。 父组件.ts 可重复使用...


typescript .d.ts 文件无法被 typescript 识别,但 vsc

更新: 它不是打字稿,而是 ts-node“问题” 我回答自己,所以如果你仍然对这个“问题”感兴趣 - 进一步阅读:) 对困惑感到抱歉 也许我完全有


在 Electron-vite 中包含本地 JavaScript 库的 TypeScript 无法正确编译

我正在尝试将本地库导入到 vite-electron TypeScript 项目中。请原谅我对 vite-electron 和 JS 捆绑生态系统缺乏了解,我是 JS 和 TS 开发的新手......


将三个 ts 线生成两个具有共享 x 轴的堆叠图

我有三个时间序列变量。其中两个包含在我的文件 roes.cvs 中,并使用以下代码绘制在一张图中: 鱼子 <- read.csv(file="roes.csv", header=TRUE, sep=",&q...


有没有办法根据输入数据自动预测模型(如分类、回归、TS)?

我想知道我们根据输入数据预测模型的条件 可以根据输入数据自动预测模型(时间序列、回归、分类)。 任何李...


在JS/TS中构造RSA公钥

我有一个 C# Web api,它将公钥传递给 React 应用程序。我需要使用该公钥来加密有效负载/消息,然后将其发送回我的 api。但是,我在重建时遇到了麻烦...


Cucumber with playwright 无法识别 ES2022 模块配置中的 .TS 文件

我正在尝试以这种方式通过剧作家测试来实现黄瓜: 有一个 cucumber.cjs 文件,它将成为我的跑步者 有测试 > 验收 > 功能 > {这里是所有 .features 文件} 有...


嵌套通用 TS - 将参数设置为“and”而不是“or”

我正在尝试在我的项目中创建人类功能挂钩 我使用泛型来决定并保持每个可用函数的挂钩 我不想设置所有潜在的功能,只有当我们...


如何解决“未找到规则‘@typescript-eslint/rule-name’的定义”

我正在尝试将 TypeScript 编译添加到现有的 Javascript 项目中。 据我所知,这应该是可能的(甚至很容易),并且您可以通过代码库逐步传播 TS。不幸的是


声明文件中的类型继承

我正在声明其他元素类型应该满足的“父”COMPONENT 类型。如果我在普通的 .ts 或 .tsx 文件中使用下面的代码,它可以正常工作(也就是说,它会显示我无法使用...


使用keyof为同一级别的嵌套键添加输入

与一个小组一起开发一个游戏,它有一个小百科全书。我能够从 JS 到 TS 得到一些,我正在慢慢学习。但结构已经改变。假设结构看起来像...


在卡尔达诺上使用策略脚本创建 NFT

我正在尝试在卡尔达诺上创建资产/nft 之前运行验证。我已经使用 plu-ts 编写了策略脚本,并使用 mesh sdk 编写了事务。 脚本 导出常量 mintNFT = pfn( [数据.类型,


如何解决“断言要求调用目标中的每个名称都使用显式类型注释进行声明.ts(2775)”?

我有下面的 JavaScript 代码,并且我正在使用 TypeScript 编译器 (TSC) 根据 Typescript 文档 JSDoc 参考提供类型检查。 const 断言 = require('断言'); 常量摩卡=重新...


如何处理 Vite 项目中同一个库的 3 个版本冲突?

我有以下奇怪的情况:我正在使用 Vite 和 svelte-ts 模板设置开发一个网络应用程序。在此应用程序中,我必须为基于 WebXR 的增强现实使用不同的库,这样您就可以


我是机器学习新手,我有一个问题,有没有什么方法可以根据输入数据自动预测模型(如分类、回归、TS)?

我想知道我们根据输入数据预测模型的条件 可以根据输入数据自动预测模型(时间序列、回归、分类)。 任何李...


从另一个 .ts 文件导入具有可重用 tailwindCSS 类的对象并在 Nextjs React 组件中使用其键值似乎不起作用

我正在尝试导入一个包含可重用 TailwindCSS 类的对象,并在我的应用程序中重用这些类。由于某种原因,我需要在本地复制+粘贴这个对象到反应组件中...


在 HTML 文档中嵌入 TypeScript 代码

是否可以在网页中嵌入 TypeScript 代码?我想将 TypeScript 代码嵌入到脚本标签中,如下所示(以便它自动编译为 Javascript): <p>是否可以在网页中嵌入 TypeScript 代码?我想将 TypeScript 代码嵌入到脚本标签中,如下所示(以便它自动编译为 Javascript):</p> <pre><code>&lt;script type = &#34;text/typescript&#34;&gt; //TypeScript code goes here &lt;/script&gt; </code></pre> </question> <answer tick="true" vote="28"> <p>实际上有几个项目允许您使用类似的 TypeScript 代码 - <a href="https://github.com/niutech/typescript-compile" rel="noreferrer">TypeScript Compile</a>、<a href="https://github.com/ComFreek/ts-htaccess" rel="noreferrer">ts-htaccess</a>。</p> <p>这里的问题是 .ts 代码应该编译成 JavaScript - 它可以在客户端完成(速度慢;整个 TSC 也应该加载到客户端)或在服务器端完成(显然更快,而且它更快)在编译代码上利用缓存要容易得多)。</p> </answer> <answer tick="false" vote="18"> <p>这是我编写的版本,<strong>直接</strong>使用 Microsoft/TypeScript/master 的版本,因此它始终保持最新:<a href="https://github.com/basarat/typescript-script" rel="noreferrer">https://github.com/basarat/typescript-script</a></p> <p>您甚至可以将 <pre><code>ts</code></pre> 指向您可能拥有的任何其他 TypeScript 版本,它会正常工作 🌹</p> </answer> <answer tick="false" vote="6"> <p>已经为此目的开发了一个 JavaScript 库 - 它称为 <a href="https://github.com/niutech/typescript-compile" rel="noreferrer">TypeScript Compile</a>,它允许将 Typescript 嵌入到 HTML 中(如上所示。)</p> </answer> <answer tick="false" vote="0"> <p>我写这篇文章的目的是为了在浏览器中编译 TypeScript,以便我可以编写快速简单的示例来分享。</p> <p><a href="https://github.com/Sean-Bradley/text-typescript" rel="nofollow noreferrer">https://github.com/Sean-Bradley/text-typescript</a></p> <p>使用,</p> <pre><code>&lt;script type=&#34;text/typescript&#34;&gt; // Your TypeScript code here &lt;/script&gt; </code></pre> <p>并包含依赖项。</p> <pre><code>&lt;script src=&#34;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" data-cfemail="67131e17021404150e1713275249544954">[email protected]</a>&#34;&gt;&lt;/script&gt; &lt;script defer src=&#34;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" data-cfemail="3743524f431a434e47524454455e4743770619041907">[email protected]</a>&#34;&gt;&lt;/script&gt; </code></pre> <p>一个完整的示例,您可以复制/粘贴到 HTML 文档中并在本地尝试。</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&#34;en&#34;&gt; &lt;head&gt; &lt;meta charset=&#34;utf-8&#34; /&gt; &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1&#34; /&gt; &lt;title&gt;&#34;text/typescript&#34; example&lt;/title&gt; &lt;meta name=&#34;description&#34; content=&#34;Transpiling and executing TypeScript in the browser&#34; /&gt; &lt;style&gt; body { overflow: hidden; margin: 0px; font-size: 15vw; } &lt;/style&gt; &lt;script type=&#34;text/typescript&#34;&gt; function foo(bar: string) { return &#34;Hello &#34; + bar; } let baz = &#34;World!&#34;; document.getElementById(&#34;root&#34;).innerHTML = foo(baz); &lt;/script&gt; &lt;script src=&#34;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" data-cfemail="13676a63766070617a636753263d203d20">[email protected]</a>&#34;&gt;&lt;/script&gt; &lt;script defer src=&#34;https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" data-cfemail="ec98899498c198959c899f8f9e859c98acddc2dfc2dc">[email protected]</a>&#34;&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id=&#34;root&#34;&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>你可以看到它在这里、现在、今天发挥作用。</p> <p><a href="https://editor.sbcode.net/f1f4b5a73ec40283d1ddb37bb1e71f7e4e31b487" rel="nofollow noreferrer">https://editor.sbcode.net/f1f4b5a73ec40283d1ddb37bb1e71f7e4e31b487</a></p> </answer> </body></html>


ESLint:解析错误:意外的标记:

大家好我正在将我的 vue3 项目从 js 迁移到 typescript,我遇到了这个问题: 这是我在 .vue 文件中的代码 const toto = (msg: string) => { </desc> <question vote="7"> <p>大家好,我正在将我的 vue3 项目从 js 迁移到 typescript,我遇到了这个问题:</p> <p><a href="https://i.stack.imgur.com/y5tG8.png" target="_blank"><img src="https://cdn.txt58.com/i/AWkuc3RhY2suaW1ndXIuY29tL3k1dEc4LnBuZw==" alt=""/></a></p> <p>这是我在 .vue 文件中的代码</p> <pre><code>&lt;script setup lang=&#34;ts&#34;&gt; const toto = (msg: string) =&gt; { console.log(msg) } &lt;/script&gt; </code></pre> <p>这是我的 eslintrc.js</p> <pre><code>module.exports = { &#39;env&#39;: { &#39;browser&#39;: true, &#39;es2021&#39;: true }, &#39;extends&#39;: [ &#39;eslint:recommended&#39;, &#39;plugin:vue/vue3-essential&#39; ], &#39;parserOptions&#39;: { &#39;ecmaVersion&#39;: 13, &#39;sourceType&#39;: &#39;module&#39; }, &#39;plugins&#39;: [ &#39;vue&#39; ], &#39;rules&#39;: { &#39;vue/multi-word-component-names&#39;: &#39;off&#39;, &#39;vue/object-curly-spacing&#39;: [2, &#39;always&#39;], &#39;vue/html-closing-bracket-spacing&#39;: [2, { &#39;selfClosingTag&#39;: &#39;always&#39; }], &#39;vue/max-attributes-per-line&#39;: [2, { &#39;singleline&#39;: { &#39;max&#39;: 1 }, &#39;multiline&#39;: { &#39;max&#39;: 1 } }], &#39;semi&#39;: [2, &#39;never&#39;] } } </code></pre> </question> <answer tick="true" vote="10"> <p>您需要配置 eslint 以支持 typescript,因为 eslint 不支持开箱即用。 首先,您需要安装<a href="https://www.npmjs.com/package/@typescript-eslint/parser" rel="nofollow noreferrer">@typescript-eslint/parser</a>,然后安装<a href="https://www.npmjs.com/package/@typescript-eslint/eslint-plugin" rel="nofollow noreferrer">@typescript-eslint/eslint-plugin</a>。 安装完这些后,请按如下方式更新您的配置 - </p> <pre><code>module.exports = { &#39;env&#39;: { &#39;browser&#39;: true, &#39;es2021&#39;: true, node: true }, &#39;extends&#39;: [ &#39;eslint:recommended&#39;, &#39;plugin:vue/vue3-essential&#39; ], &#39;parserOptions&#39;: { &#39;ecmaVersion&#39;: 12, &#39;sourceType&#39;: &#39;module&#39;, parser: &#39;@typescript-eslint/parser&#39; }, &#39;plugins&#39;: [ &#39;vue&#39;, &#39;@typescript-eslint&#39; ], &#39;rules&#39;: { &#39;vue/multi-word-component-names&#39;: &#39;off&#39;, &#39;vue/object-curly-spacing&#39;: [2, &#39;always&#39;], &#39;vue/html-closing-bracket-spacing&#39;: [2, { &#39;selfClosingTag&#39;: &#39;always&#39; }], &#39;vue/max-attributes-per-line&#39;: [2, { &#39;singleline&#39;: { &#39;max&#39;: 1 }, &#39;multiline&#39;: { &#39;max&#39;: 1 } }], &#39;semi&#39;: [2, &#39;never&#39;] } } </code></pre> </answer> <answer tick="false" vote="1"> <p>就我而言,问题是我使用解析器选项作为数组,而不是字符串:</p> <pre><code> parserOptions: { - parser: [&#39;@typescript-eslint/parser&#39;], + parser: &#39;@typescript-eslint/parser&#39;, }, </code></pre> </answer> <answer tick="false" vote="0"> <p>如果你在项目中同时使用 JS 和 TS,此配置有帮助</p> <pre><code> overrides: [ { files: [&#39;*.vue&#39;], parser: &#39;svelte-eslint-parser&#39;, parserOptions: { parser: { // Specify a parser for each lang. ts: &#39;@typescript-eslint/parser&#39;, js: &#39;espree&#39;, typescript: &#39;@typescript-eslint/parser&#39; } } } ], </code></pre> </answer> <answer tick="false" vote="-1"> <p>我在节点 v12.22.9 上遇到了这个问题。通过升级到 v14.21.2,我不再遇到解析错误。您可以使用命令升级/安装</p> <pre><code>nvm install v14.21.2 </code></pre> </answer> </body></html>


在 Angular 中打开模态时出现ExpressionChangedAfterItHasBeenCheckedError

` `<ngx-datatable-column *ngIf="search_type ==='booking'" [width]="50" [sortable]="false" [canAutoResize]="false" [draggable]="false" [resizeable]="false"> <ng-template ngx-datatable-cell-template let-row="row" let-value="value"> <span class="fa fa-user-plus font-medium-3 text-primary cursor-pointer pl-1" (click)="onUpdateBookingLinguist(row)"></span> </ng-template> </ngx-datatable-column>` 上面是我的html代码,下面是我的ts代码 ` onUpdateBookingLinguist(linguist: Linguist) { const modalRef = this.modalService.open(ScheduledBookingModalComponent, { size: 'lg' }); modalRef.componentInstance.linguist_other_bookings = linguist['scheduled_booking']; modalRef.result.then((result) => { if (!result) { return; } this.cdr.detectChanges(); }).catch((e) => { }); this.cdr.detectChanges(); }` 我试图从 ngx-datatable-column 打开模式,但收到此错误 错误错误:NG0100:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。上一个值:“datatable-body-cell sort-active active”。当前值:'datatable-body-cell sort-active'.. 欲了解更多信息,请访问 https://angular.io/errors/NG0100 我尝试使用 ChangeDetectorRef.detectChanges() 手动触发更改检测,但仍然收到错误。 当您修改 Angular 在其更改检测周期期间也尝试检查或更新的属性时,通常会出现此错误。 因此您可以使用 ChangeDetectorRef 在特定情况下手动触发更改检测。通过从 ChangeDetectorRef 调用 detectorChanges(),您可以手动指示 Angular 对该组件及其子组件重新运行更改检测,从而可能解决问题 这是如何在组件中使用ChangeDetectorRef 的示例 构造函数(私有 cdr:ChangeDetectorRef){} ngAfterContentChecked() { this.cdr.detectChanges(); }


绑定了formControlName后,如何在html中使用它?

我当前有问题的 html 部分是这样的 ... 我的 html 当前有问题的部分是这个 <div class="psi-list"> <div class="flex-row" *ngFor="let item of psiList; index as i"> <p class="row-psi">{{ item.psi }}</p> <button class="icon-btn" (click)="deletePsi(item.psi)"><i class="cil-trash"></i></button> <button class="icon-btn" (click)="item.toggle = !item.toggle"><i class="cil-pen-alt"></i></button> <form [formGroup]="psiEditForm" id="psi-edit-form" class="form-horizontal" [hidden]="!item.toggle"> <input class="psi-input" type="text" placeholder="Update PSI here..." [(ngModel)]="psiEdit" [ngClass]="{'is-valid': !checkControlValidation(psiEditForm.controls.psi) && psiEditForm.controls.psi.touched, 'is-invalid': checkControlValidation(psiEditForm.controls.psi)}" (keydown.enter)="!psiEditForm.controls.psi.invalid ? editPsi(item.psi) : clearPsiEdit()" formControlName="psi" /> <div class="help-block validation-warning" *ngIf="checkControlValidation(psiEditForm.controls.psi)"><i class="fa fa-exclamation fa-lg"></i>Please Enter a Valid 2 Letter PSI</div> </form> </div> </div> 我使用 formControlName“psi”将控件传递给 .ts 文件中的 checkControlValidation 方法。然而,这会导致功能问题。每当我在一个输入框中输入内容时,它都会将其输入到 *ngFor 中的所有输入框中。 当我将表单控件名称更改为这个时 - [formControlName]="item.psi" 它将名称绑定到与其相关的项目,因此理论上我可以通过它来解决我的问题。但是,当我将 psiEditForm.controls.psi 的所有实例更改为 psiEditForm.controls.item.psi 时,我收到很多 TS2339 错误。 我有什么想法可以使用它吗?抱歉,我知道这可能措辞很糟糕 尝试这样 <div class="psi-list"> <div class="flex-row" *ngFor="let item of psiList; index as i"> <p class="row-psi">{{ item.psi }}</p> <button class="icon-btn" (click)="deletePsi(item.psi)"><i class="cil-trash"></i></button> <button class="icon-btn" (click)="item.toggle = !item.toggle"><i class="cil-pen-alt"></i></button> <form [formGroup]="psiEditForm" id="psi-edit-form" class="form-horizontal" [hidden]="!item.toggle"> <input class="psi-input" type="text" placeholder="Update PSI here..." [(ngModel)]="psiEdit" [ngModelOptions]="{standalone: true}" [ngClass]="{'is-valid': !checkControlValidation(psiEditForm.controls.psi) && psiEditForm.controls.psi.touched, 'is-invalid': checkControlValidation(psiEditForm.controls.psi)}" (keydown.enter)="!psiEditForm.controls.psi.invalid ? editPsi(item.psi) : clearPsiEdit()" formControlName="psi" /> <div class="help-block validation-warning" *ngIf="checkControlValidation(psiEditForm.controls.psi)"> <i class="fa fa-exclamation fa-lg"></i>Please Enter a Valid 2 Letter PSI </div> </form> </div> </div>


在使用 TypeScript 进行 React 时,输入字段在“值”处给出错误

从“react”导入{SetStateAction,useState} const 登录表单 =() =>{ const [名称,setName] = useState(); const [全名,setFullname] = useState import { SetStateAction, useState } from "react" const Loginform =() =>{ const [name, setName] = useState<String | null>(); const [fullname, setFullname] = useState<String | null>(); const inputEvent =(event: { target: { value: SetStateAction< String |null | undefined >; }; })=>{ setName(event.target.value) } const Submit = ()=>{ setFullname(name) } return <> <h1>Enter Your Name </h1> <input type="text" placeholder="Enter your name" onChange={inputEvent} value={name}/> <button onClick={Submit}>Submit</button> <h1>Hi {fullname==null? "Guest" :fullname}</h1> </> } export default Loginform <input type="text" placeholder="Enter your name" onChange={inputEvent} value={name}/> 上一行中的“value”属性显示错误 这是错误详细信息: (property) React.InputHTMLAttributes<HTMLInputElement>.value?: string | number | readonly string[] | undefined Type 'String | null | undefined' is not assignable to type 'string | number | readonly string[] | undefined'. Type 'null' is not assignable to type 'string | number | readonly string[] | undefined'.ts(2322) index.d.ts(2398, 9): The expected type comes from property 'value' which is declared here on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>' 也尝试写 return as any。或者尝试添加 string[],但出现错误。 进行以下调整: 将 String 替换为 string,因为原始 Typescript 类型是小写的 (number, string, boolean, etc) TypeScript 报告此错误,因为属性值不接受 null 作为值,而是接受 undefined。将代码更新为: const [name, setName] = useState<string>() 这会使用 string 作为默认类型来初始化状态。 初始状态隐式地被视为 undefined,因为如果未显式提供,TypeScript 会将类型的默认值设置为 undefined。 为了简单起见,您不需要解构事件。使用内联函数,如下例所示: <input type="text" placeholder="Enter your name" onChange={(e) => setName(e.target.value)} value={name}/> 我使用你的代码创建了一个 CodeSanbox,它似乎只需要很少的修改就可以正常工作。 您可以在这里找到沙盒 这是我想出的代码 import { SetStateAction, useState } from "react"; const Loginform = () => { const [name, setName] = useState<string | null>(); const [fullname, setFullname] = useState<string | null>(); const inputEvent = (event: { target: { value: SetStateAction<string | null | undefined> }; }) => { setName(event.target.value); }; const Submit = () => { setFullname(name); }; return ( <div> <h1>Enter Your Name </h1> <input type="text" placeholder="Enter your name" onChange={inputEvent} value={name} /> <button onClick={Submit}>Submit</button> <h1>Hi {fullname == null ? "Guest" : fullname}</h1> </div> ); }; export default Loginform;


svelte:第一次加载时窗口内部宽度未定义

我有这个组件来检查设备大小 从“$lib/stores”导入{deviceSize}; 让内部宽度; $:如果(内部宽度> = 1652){ ...</desc> <question vote="0"> <p>我有这个组件来检查设备尺寸</p> <pre><code>&lt;script lang=&#34;ts&#34;&gt; import { deviceSize } from &#34;$lib/stores&#34;; let innerWidth; $: if (innerWidth &gt;= 1652) { $deviceSize = { xl: true, lg: false, md: false, dsm: false, sm: false, }; } else if (innerWidth &gt;= 1240 &amp;&amp; innerWidth &lt; 1652) { $deviceSize = { xl: false, lg: true, md: false, dsm: false, sm: false, }; } else if (innerWidth &gt;= 794 &amp;&amp; innerWidth &lt; 1240) { $deviceSize = { xl: false, lg: false, md: true, dsm: false, sm: false, }; } else if (innerWidth &gt;= 640 &amp;&amp; innerWidth &lt; 794) { $deviceSize = { xl: false, lg: false, md: false, dsm: true, sm: false, }; } else { $deviceSize = { xl: false, lg: false, md: false, dsm: false, sm: true, }; } $: console.log(innerWidth); &lt;/script&gt; &lt;svelte:window bind:innerWidth /&gt; </code></pre> <p>和像这样的应用程序组件</p> <p><App.svelte></p> <pre><code>&lt;script&gt; const { lg, xl } = $deviceSize; $: isDesktop = xl || lg; &lt;/script&gt; {#if isDesktop} &lt;DesktopComponent/&gt; {/if} {#if !isDesktop} &lt;MobileComponent/&gt; {/if} </code></pre> <p><a href="https://i.stack.imgur.com/6iNXn.png" target="_blank"><img src="https://cdn.txt58.com/i/AWkuc3RhY2suaW1ndXIuY29tLzZpTlhuLnBuZw==" alt="enter image description here"/></a></p> <p>我的问题是innerWidth在初始加载中总是未定义。所以 isDesktop = false,那么即使我使用桌面,也始终渲染 MobileComponent。请帮我解决这个问题。</p> <p>我尝试为 <pre><code>deviceSize</code></pre> 商店设置默认值,但无法按我想要的方式工作,它始终呈现为我使用的任何设备(PC、移动设备)的默认条件。</p> </question> <answer tick="false" vote="0"> <p>根据<a href="https://svelte.dev/docs/svelte-components#:%7E:text=Reactive%20statements%20run%20after%20other%20script%20code%20and%20before%20the%20component%20markup%20is%20rendered%2C" rel="nofollow noreferrer">svelte 文档</a>:</p> <blockquote> <p>反应式语句在其他脚本代码之后、渲染组件标记之前运行</p> </blockquote> <p>意味着 if-else 块在创建 <pre><code>svelte:window</code></pre> 绑定之前运行一次,此时 <pre><code>innerWidth</code></pre> 未定义。</p> <p>为了避免这种情况,您可以将 <pre><code>innerWidth</code></pre> 初始化为正确的值,例如更换</p> <pre><code>let innerWidth; </code></pre> <p>与</p> <pre><code>let innerWidth = window.innerWidth; </code></pre> <p>也就是说,通过使用 <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries" rel="nofollow noreferrer">CSS 媒体查询</a>(而不是 JavaScript)来显示和隐藏标记,您可能会让您的生活变得更轻松。</p> </answer> </body></html>


向垫子表添加额外的行

所以我有一张垫子桌 所以我有一张垫子桌 <mat-table class="table" cdkDropList cdkDropListOrientation="horizontal" (cdkDropListDropped)="tableDrop($event)" [dataSource]="tableDataSource"> <ng-container *ngFor="let column of columns; let i = index" [matColumnDef]="column.name"> <mat-header-cell *matHeaderCellDef cdkDrag dkDragLockAxis="x" cdkDragBoundary="mat-header-row"> {{ column.title }} </mat-header-cell> <mat-cell *matCellDef="let element"> {{ element[column.name] }} </mat-cell> </ng-container> <mat-header-row class="tableHeader" *matHeaderRowDef="tableDisplayedColumns" #tableHeaderRow> </mat-header-row> <mat-row class="tableRow" *matRowDef="let row; columns: tableDisplayedColumns;" [class.selected-row]="tableSelectedRows.has(row)" (click)="selectUnselectRow(row)"> </mat-row> </mat-table> 但我需要在表标题下为相应的行过滤器添加一行。我尝试在标题和实际行声明之间添加 <mat-row> ,但是由于过滤器是不同的输入(例如数字、自动完成选择和多选),我无法 *ngFor 它们(而且我不是当然我是否能够) 编辑:忘记发布过滤器 HTML <div class="filterGroup"> <mat-form-field class="filterField"> <input matInput type="number" (keydown)="updateManualPage(1)" placeholder="Filter za param1" formControlName="filterParam1"> </mat-form-field> <mat-form-field class="filterField"> <input matInput (keydown)="updateManualPage(1)" placeholder="Filter za param2" formControlName="filterParam2" [matAutocomplete]="autoSingleSelect"> <mat-autocomplete #autoSingleSelect="matAutocomplete" class="filterSelect" panelClass="filterSelect"> <mat-option *ngFor="let option of dropdownSingleFilteredOptions | async" [value]="option.param2"> {{option.param2}} </mat-option> </mat-autocomplete> </mat-form-field> <mat-form-field class="filterField"> <mat-select class="filterMultiselect" placeholder="Filter za param3" formControlName="filterParam3" multiple panelClass="filterMultiselect"> <mat-option *ngFor="let option of tableDataSource.data" [value]="option.param3"> {{option.param3}} </mat-option> </mat-select> </mat-form-field> </div> 以及相关组件.ts tableDisplayedColumns: string[] = ['param1', 'param2', 'param3']; columns: any[] = [ { name: 'param1', title: 'Param1' }, { name: 'param2', title: 'Param2' }, { name: 'param3', title: 'Param3' } ]; 为了解决这个问题,我设法通过删除 *ngFor 并手动放入过滤器来做到这一点。 <mat-table class="table" cdkDropList cdkDropListOrientation="horizontal" (cdkDropListDropped)="tableDrop($event)" [dataSource]="tableDataSource"> <ng-container matColumnDef="param1"> <mat-header-cell *matHeaderCellDef cdkDrag cdkDragLockAxis="x" cdkDragBoundary="mat-header-row" [cdkDragStartDelay]="100"> Param1 <mat-form-field class="filterField"> <input matInput type="number" (keydown)="updateManualPage(1)" placeholder="Filter" formControlName="filterParam1"> </mat-form-field> </mat-header-cell> <mat-cell *matCellDef="let data"> <span>{{data.param1}}</span> </mat-cell> </ng-container> <ng-container matColumnDef="param2"> <mat-header-cell *matHeaderCellDef cdkDrag cdkDragLockAxis="x" cdkDragBoundary="mat-header-row" [cdkDragStartDelay]="100"> Param2 <mat-form-field class="filterField"> <input matInput (keydown)="updateManualPage(1)" placeholder="Filter" formControlName="filterParam2" [matAutocomplete]="autoSingleSelect"> <mat-autocomplete #autoSingleSelect="matAutocomplete" class="filterSelect" panelClass="filterSelect"> <mat-option *ngFor="let option of dropdownSingleFilteredOptions | async" [value]="option.param2"> {{option.param2}} </mat-option> </mat-autocomplete> </mat-form-field> </mat-header-cell> <mat-cell *matCellDef="let data"> <span>{{data.param2}}</span> </mat-cell> </ng-container> <ng-container matColumnDef="param3"> <mat-header-cell *matHeaderCellDef cdkDrag cdkDragLockAxis="x" cdkDragBoundary="mat-header-row" [cdkDragStartDelay]="100"> Param3 <mat-form-field class="filterField"> <mat-select class="filterMultiselect" placeholder="Filter" formControlName="filterParam3" multiple panelClass="filterMultiselect"> <mat-option *ngFor="let option of tableDataSource.data" [value]="option.param3"> {{option.param3}} </mat-option> </mat-select> </mat-form-field> </mat-header-cell> <mat-cell *matCellDef="let data"> <span>{{data.param3}}</span> </mat-cell> </ng-container> <mat-header-row class="tableHeader" *matHeaderRowDef="tableDisplayedColumns" #tableHeaderRow> </mat-header-row> <mat-row class="tableRow" *matRowDef="let row; columns: tableDisplayedColumns;" [class.selected-row]="tableSelectedRows.has(row)" (click)="selectUnselectRow(row)"> </mat-row> </mat-table>


© www.soinside.com 2019 - 2024. All rights reserved.