2

I have the following html code

<head>
    <script src="https://unpkg.com/vue@3"></script>
    <script defer src="script.js"></script>
</head>
<body>
    <div id="main">        
        <div>
            1. This is 1st line

            2. This is 2nd line

            3. This is 3rd line
        </div>
        <br>
        <div style="white-space: pre-line;">
            1. This is 1st line

            2. This is 2nd line

            3. This is 3rd line
        </div>
    </div>
</body>

Which will result in

1. This is 1st line 2. This is 2nd line 3. This is 3rd line


1. This is 1st line

2. This is 2nd line

3. This is 3rd line

However, when I mount a Vue instance onto my main div, this is the result I get

1. This is 1st line 2. This is 2nd line 3. This is 3rd line

1. This is 1st line 2. This is 2nd line 3. This is 3rd line

The code for Vue instance in the script.js file is as follow

const test = Vue.createApp({

}).mount("#main")

Why did my white-space style get ignored completely?

2
  • This is probably a matter of how the compiler takes the various pieces of HTML and glues them together. pre-line is taking the new line into account but it's probably not exactly the same between the behavior of the browser and what JS is doing with it. Still, it quite a minor problem here, since you could fix it by adding a <br /> or even wrapping your lines into other parents. Not a big deal worth spending much time on IMO. TLDR: it's not ignored, just the compiler doing it's own stuff VS browser's default behavior. Commented May 16, 2022 at 17:16
  • 1
    @tony19 codepen.io/NepNepFFXIV/pen/oNEWLgz?editors=1111 Commented May 18, 2022 at 18:27

1 Answer 1

2

The Vue compiler collapses whitespace by default, so the newlines in your original code gets collapsed (extraneous whitespace is removed) to produce more efficient compiled output.

Option 1: App config to disable whitespace-condense

You can disable this globally in your example with app.config.compilerOptions.whitespace set to 'preserve':

const app = Vue.createApp({})
app.config.compilerOptions.whitespace = 'preserve'
app.mount("#main")

demo 1

Or disable it per component:

const app = Vue.createApp({
  compilerOptions: {
    whitespace: 'preserve'
  }
})
app.mount("#main")

demo 2

Note: app.config.compilerOptions.whitespace is only respected when using the full build. Otherwise, you'd have to set the option through build flags.

Option 2: Build flag to disable whitespace-condense

You can configure @vue/compiler-sfc to disable whitespace-condense in this Vite config:

// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          whitespace: 'preserve', 👈
        },
      },
    }),
  ],
})

demo 3

Option 3: Use <br> where new-line is needed

Alternatively, you could explicitly add <br> tags where needed, which would keep the originally intended optimization while implementing the desired spacing:

<div>
  1. This is 1st line<br>

  2. This is 2nd line<br>

  3. This is 3rd line<br>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.