[英]Why does Vue ignore css rule?
I have the following html code我有以下 html 代码
<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然而,当我将一个 Vue 实例挂载到我的主 div 上时,这就是我得到的结果
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 script.js文件中的Vue实例代码如下
const test = Vue.createApp({
}).mount("#main")
Why did my white-space style get ignored completely?为什么我的空白样式被完全忽略了?
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. Vue 编译器默认折叠空格,因此原始代码中的换行符会折叠(多余的空格被删除)以生成更高效的编译 output。
You can disable this globally in your example with app.config.compilerOptions.whitespace
set to 'preserve'
:您可以在您的示例中全局禁用此功能,并将
app.config.compilerOptions.whitespace
设置为'preserve'
:
const app = Vue.createApp({})
app.config.compilerOptions.whitespace = 'preserve'
app.mount("#main")
Or disable it per component:或者按组件禁用它:
const app = Vue.createApp({
compilerOptions: {
whitespace: 'preserve'
}
})
app.mount("#main")
Note: app.config.compilerOptions.whitespace
is only respected when using the full build.注意:
app.config.compilerOptions.whitespace
仅在使用完整构建时才受到尊重。 Otherwise, you'd have to set the option through build flags.否则,您必须通过构建标志设置选项。
You can configure @vue/compiler-sfc
to disable whitespace-condense in this Vite config:你可以在这个 Vite 配置中配置
@vue/compiler-sfc
来禁用 whitespace-condense:
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [
vue({
template: {
compilerOptions: {
whitespace: 'preserve', 👈
},
},
}),
],
})
<br>
where new-line is needed<br>
Alternatively, you could explicitly add <br>
tags where needed, which would keep the originally intended optimization while implementing the desired spacing:或者,您可以在需要的地方显式添加
<br>
标签,这将在实现所需间距的同时保持最初预期的优化:
<div>
1. This is 1st line<br>
2. This is 2nd line<br>
3. This is 3rd line<br>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.