属性 'replaceAll' 在类型 'string' 上不存在
Property 'replaceAll' does not exist on type 'string'
我想在打字稿中使用 replaceAll
和 angular 10.
但我收到此错误:属性 'replaceAll' 在类型 'string' 上不存在。
这是我的代码:
let date="1399/06/08"
console.log(date.replaceAll('/', '_'))
输出:13990608
如何修复我的打字稿以显示此功能?
来自docs:
As of August 2020 the replaceAll()
method is supported by Firefox but
not by Chrome. It will become available in Chrome 85.
与此同时,您还可以找到多种其他方法 here。
供未来可能的读者使用的屏幕截图:
您可以使用 RegExp 和 global flag 解决问题。全局标志是 replace
运行 在所有事件中的原因。
"1399/06/08".replace(/\//g, "_") // "1399_06_08"
Chrome 支持 replaceAll 所以可以安全使用。然而打字稿仍然会报错,所以你可以将你的字符串转换为任何,以克服这个障碍。
const date: any ="1399/06/08"
console.log(date.replaceAll('/','_'))
就用这个功能
let date="1399/06/08"
console.log(date.split('/').join('_'))
您可以创建一个文件
myOwnTypes.d.ts
在您的 angular 项目的根目录并添加以下代码:
interface String {
replaceAll(input: string, output : string): any;
}
这将告诉打字稿字符串有这个 属性。
现在 replaceAll 在 Chrome 和 Firefox 中受支持,但最好检查 caniuse 以检查它是否符合您的需要。
https://caniuse.com/?search=replaceAll
如果这对你有用,欢迎投票,我从这个 Whosebug 帐户开始,希望得到支持:)
根据 MDN 网络文档,
"to perform a global search and replace, include the g
switch in the
regular expression".
所以,你可以尝试这样做:
const date="1399/06/08"
const forwardSlashRegex = /(\/)/g;
console.log(date.replace(forwardSlashRegex, '_'));
这会自动将所有正斜杠替换为下划线。确保还保留正则表达式末尾的 /g
全局指示符,因为它允许 JS 知道您要替换出现正斜杠的所有位置。
有关使用正则表达式指标的更多信息,请参阅以下非常有用的指南:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
您应该可以通过 tsconfig.json
添加这些类型。
在 compilerOptions
.
内添加 "ES2021.String"
到 lib
你的 tsconfig 应该看起来像这样:
{
...,
"compilerOptions": {
...,
"lib": [
...,
"ES2021.String"
]
}
}
replaceAll
方法在lib.es2021.string.d.ts
内部定义如下:
interface String {
/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replaceAll(searchValue: string | RegExp, replaceValue: string): string;
/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replacer A function that returns the replacement text.
*/
replaceAll(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
}
我想在打字稿中使用 replaceAll
和 angular 10.
但我收到此错误:属性 'replaceAll' 在类型 'string' 上不存在。
这是我的代码:
let date="1399/06/08"
console.log(date.replaceAll('/', '_'))
输出:13990608
如何修复我的打字稿以显示此功能?
来自docs:
As of August 2020 the
replaceAll()
method is supported by Firefox but not by Chrome. It will become available in Chrome 85.
与此同时,您还可以找到多种其他方法 here。
供未来可能的读者使用的屏幕截图:
您可以使用 RegExp 和 global flag 解决问题。全局标志是 replace
运行 在所有事件中的原因。
"1399/06/08".replace(/\//g, "_") // "1399_06_08"
Chrome 支持 replaceAll 所以可以安全使用。然而打字稿仍然会报错,所以你可以将你的字符串转换为任何,以克服这个障碍。
const date: any ="1399/06/08"
console.log(date.replaceAll('/','_'))
就用这个功能
let date="1399/06/08"
console.log(date.split('/').join('_'))
您可以创建一个文件
myOwnTypes.d.ts
在您的 angular 项目的根目录并添加以下代码:
interface String {
replaceAll(input: string, output : string): any;
}
这将告诉打字稿字符串有这个 属性。
现在 replaceAll 在 Chrome 和 Firefox 中受支持,但最好检查 caniuse 以检查它是否符合您的需要。
https://caniuse.com/?search=replaceAll
如果这对你有用,欢迎投票,我从这个 Whosebug 帐户开始,希望得到支持:)
根据 MDN 网络文档,
"to perform a global search and replace, include the
g
switch in the regular expression".
所以,你可以尝试这样做:
const date="1399/06/08"
const forwardSlashRegex = /(\/)/g;
console.log(date.replace(forwardSlashRegex, '_'));
这会自动将所有正斜杠替换为下划线。确保还保留正则表达式末尾的 /g
全局指示符,因为它允许 JS 知道您要替换出现正斜杠的所有位置。
有关使用正则表达式指标的更多信息,请参阅以下非常有用的指南:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
您应该可以通过 tsconfig.json
添加这些类型。
在 compilerOptions
.
"ES2021.String"
到 lib
你的 tsconfig 应该看起来像这样:
{
...,
"compilerOptions": {
...,
"lib": [
...,
"ES2021.String"
]
}
}
replaceAll
方法在lib.es2021.string.d.ts
内部定义如下:
interface String {
/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replaceAll(searchValue: string | RegExp, replaceValue: string): string;
/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replacer A function that returns the replacement text.
*/
replaceAll(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
}