我想用随机的消息欢迎我的用户,但是在每条消息中,它仍然必须将用户的名字引用给用户。
因此,我想创建一系列的流行语,例如:
const CATCHPHRASES = [
`Hi ${user}, how you doin' ?`,
`Have a great day, ${user} !`,
`Sun is shining, so is your day ${user} !`
];
并具有随机提供给我的功能,可将用户作为道具
function getIntroTextRandomized(username: string): string {
// Somehow filling the CATCHPHRASES here with my username props
return CATCHPHRASES[Math.floor(Math.random() * CATCHPHRASES.length)];
}
除了es6字符串插值,我想不到其他解决方案。
谢谢你的帮助
最佳答案
也许您必须使用replace
功能?
const CATCHPHRASES = [
`Hi :user, how you doin' ?`,
`Have a great day, :user !`,
`Sun is shining, so is your day :user !`
];
function getIntroTextRandomized(username: string): string {
// Somehow filling the CATCHPHRASES here with my username props
return CATCHPHRASES[Math.floor(Math.random() * CATCHPHRASES.length)].replace(`:user`, username);
}