当使用 addMethod 函数将自定义方法添加到 yup 实例时,会产生以下错误
TS2339:类型“typeof import("node_modules/yup/lib/index")”上不存在属性“title”
重现
yupInstance.ts 文件
import * as yup from 'yup';
function defaultTitleValidation(this: any, local: 'en' | 'bn') {
return this.string().trim().required();
}
yup.addMethod(yup.string, 'title', defaultTitleValidation);
export default yup;
Run Code Online (Sandbox Code Playgroud)
common.d.ts 文件
declare module 'yup' {
interface StringSchema<TIn, TContext, TOut> {
title(local: 'en' | 'bn'): any;
}
}
Run Code Online (Sandbox Code Playgroud)
myImplementationComponent.tsx
import yup from '../../../../common/yup';
const validationSchema = yup.object().shape({
title_en: yup.title(), // TS2339: Property 'title' does not exist on type 'typeof import("node_modules/yup/lib/index")'
});
Run Code Online (Sandbox Code Playgroud) 单击一个按钮(页面底部),我想转到当前页面顶部的某个元素(在我的情况下,#navbar),但我不知道该怎么做.我试过以下代码但没有用.
<nav class="navbar navbar-light bg-faded" id="navbar">
<a class="navbar-brand" href="#">
{{appTitle}}
</a>
<!-- rest of the nav link -->
</nav>
<!-- rest of the page content -->
<!-- bottom of the page -->
<button class="btn btn-outline-secondary" (click)="gotoTop()">Top</button>
Run Code Online (Sandbox Code Playgroud)
在角度分量中:
import { Router } from '@angular/router';
/* rest of the import statements */
export class MyComponent {
/* rest of the component code */
gotoTop(){
this.router.navigate([], { fragment: 'navbar' });
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人帮我解决问题并解释了为什么我的代码无效,我将非常感激.
请注意,元素(navbar)位于其他组件中.
我有一个脚本,我从我的服务器加载到我的父HTML头...我想在重新加载时将该脚本插入到我的iframe中.
我有一个函数,使我的iframe重新加载之前,它将脚本插入头像这样: -
function insertScriptIntoHead(){
var head = iframe.contentWindow.document.getElementsByTagName('head')[0];
var script = iframe.contentWindow.document.createElement('script');
script.src = getFromParent();
script.type = 'text/javascript';
head.appendChild(script);
}
Run Code Online (Sandbox Code Playgroud)
这个文件是1.5 MB,每次重新加载它再次加载,我想消除.
我想如果我可以从父加载它并将文件放在每次重新加载到iframe然后我可能会消除从服务器加载的文件,这将节省时间.
任何帮助将不胜感激
-谢谢
在Angular 4+中,我遵循模板驱动的形式:
<form #addForm="ngForm" (ngSubmit)="onFormSubmit($event, addForm)" >
<div class="form-group">
<label for="item_name">Item Name</label>
<input id="item_name" name="item_name" [(ngModel)]="itemName"
#item_name="ngModel" autofocus class="form-control"
required minlength="4" maxlength="20"
aria-describedby="itemNameHelpBlock">
<small *ngIf="(item_name.invalid && item_name.touched)" id="itemNameHelpBlock" class="form-text text-error" >
Value must be at least 4 characters and must not exceed 20 characters
</small>
</div>
<div class="form-group">
<label for="item_type">Item Type</label>
<input id="item_type" name="item_type" [(ngModel)]="itemType"
#item_type="ngModel" class="form-control" required minlength="2"
maxlength="20" aria-describedby="itemTypeHelpBlock">
<small *ngIf="(item_type.invalid && item_type.touched)" id="itemTypeHelpBlock" class="form-text text-error" >
Value must be at least 2 characters and must not exceed …
Run Code Online (Sandbox Code Playgroud)