Keep spaces in strings in vue
Sometimes some requirements require youreserveThe string returned by the backendSpaces, when we use instructionsv-html
When the string returned by the backend is directly rendered to the page, it willAutomatic clearanceLose spaces in strings
Solution
Style processing white-space: pre-wrap
<template> <!-- useCSSKeep spaces in style --> <p :style="{ whiteSpace: 'pre-wrap' }">{{ content }}</p> </template>
content
It's the string with spaces you want to render
2. Convert spaces to entities ‘’ -> ‘ ’
<template> <!-- useHTMLStay spaces --> <p>{{ (/\s/g, '&nbsp;') }}</p> </template>
Enter space in vue & enter space in html
Enter space in vue
Vue input multiple spaces workaround (as is uni-app)
'nbsp' | '\u00a0' | A line of blank spaces, the width occupied by the space is affected by the font (one character width). |
---|---|---|
'ensp' | '\u2002' | Type half of the "space" key (half of the Chinese character width, one character width) in a full-width state. |
'emsp' | '\u2003' | Type the "space" key in a full-width state (the width of 1 Chinese character, the width of two characters). |
example:
<text class="txt">Prepare<text v-html="'\u2003\u2003'"></text>Note</text>
Enter spaces in html
example:
<div class="txt">Prepare&emsp;Note</text>
This is the end of this article about retaining spaces in strings in vue. For more related contents of retaining strings in vue, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!