我不知道是否有人还在处理这个问题,我已经写了一个快速修复这个问题。它并不完美,但可以胜任。
您将通过一个自动添加换行符的方法运行文本。
<div
style="
font-size: 30pt;
margin: auto;
color: white;
max-width: 600px;
"
ref="theRef"
>
<vue-typer
v-if="startTypers"
:text="[
formatText(
'TextHere',
'theRef',
22
),
]"
:repeat="0"
:shuffle="false"
initial-action="typing"
:pre-type-delay="70"
:type-delay="70"
:pre-erase-delay="2000"
:erase-delay="100"
erase-style="backspace"
:erase-on-complete="false"
caret-animation="blink"
></vue-typer>
</div>
mounted() {
setTimeout(() => {
this.startTypers = true;
}, 150);
}
使用 startTypers 的原因是因为它们会在 div 被渲染之前运行 formatText 方法。这意味着您将无法获取父 div 的 clientWidth。
formatText(text, ref, textSize = 22) {
let maxChars = Math.floor(this.$refs[ref].clientWidth / textSize);
let words = text.split(" ");
let breaked = "";
let currentCount = 0;
words.forEach((word) => {
currentCount += word.length;
currentCount += 1;
if (currentCount >= maxChars) {
currentCount = word.length;
breaked = `${breaked}\n${word} `;
} else {
breaked = `${breaked}${word} `;
}
});
return breaked;
},
formatText 的参数是要添加换行符的文本、引用的名称和渲染的跨度(字符)的大小(字体和字体的默认值为 22-我在我的用例中使用的尺寸,你的会有所不同)
希望这会有所帮助