Vue 字符串的格式化填充

发布时间 2024-01-03 10:37:45作者: ^ZZQJWJ^

main.js

// 字符串格式化
Vue.prototype.$stringFormat = function (formatted, ...args) {
  for (let i = 0; i < args.length; i++) {
    let regexp = new RegExp('\\{' + i + '\\}', 'gi')
    formatted = formatted.replace(regexp, args[i])
  }
  return formatted
}

使用:

Vue.prototype.$stringFormat('这是{0},这是{1}!', 'Test1', 'Test2')

返回结果:

这是Test1,这是Test2!