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!