英文:
vue styling in view does not apply to html
问题
我已经用Vue3建立了一个新项目。我开始使用一些包的实现,但后来我意识到我的组件中编写的CSS没有应用在HTML中。这是一个非常简单的视图示例:
<template>
<div>
<p class="asd">Hello</p>
</div>
</template>
<style>
.asd {
color: purple;
}
</style>
结果中文本不是紫色的。我已经检查过是否被其他CSS覆盖,但没有,它甚至不显示在开发工具的样式屏幕上:
我尝试和检查过的事项:
- 我尝试将样式写成 "p.asd {..." 并在最后添加了
!important
。没有变化。 - 我检查了是否在正确的页面上工作,将文本从 "Hello" 更改为 "Hello World",它确实变化了,所以我在正确的页面上工作。
- 如果是开发环境问题,我会得到一个生产构建,但是没有,从构建后的HTML中得到了相同的结果。
- 我尝试将CSS移动到
App.vue
中,令人惊讶的是在那里编写的样式起作用,但在视图中不起作用。 - 我尝试在新的浏览器上测试缓存问题,没有变化。
- 我尝试添加了作用域,没有变化。
- 我尝试添加了
<script>
标签,没有变化。<script>
标签正常工作,console.log
也正常工作,但样式仍然没有更新。 - 我尝试内联CSS,它起作用。
这是我的 package.json
:
{
"name": "new-project",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
"format": "prettier --write src/"
},
"dependencies": {
"express": "^4.18.2",
"firebase": "^9.22.1",
"pinia": "^2.0.36",
"vue": "^3.3.2",
"vue-router": "^4.2.0",
"vue-tel-input": "^8.1.1"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/eslint-config-prettier": "^7.1.0",
"eslint": "^8.39.0",
"eslint-plugin-vue": "^9.11.0",
"prettier": "^2.8.8",
"vite": "^4.3.5",
"vite-plugin-webfont-dl": "^3.7.4"
}
}
Codesandbox示例链接: https://codesandbox.io/p/sandbox/github/ihsanser/vue-vite-style-bug/tree/master
我不知道是否漏掉了什么非常简单的东西,但是我已经试了好几个小时,还是找不到原因。
英文:
I have set up a new project with vue3. I started working with some package implementations but then I realized my CSS written in component is not applied in HTML. Here is a very simple view example
<template>
<div>
<p class="asd">Hello</p>
</div>
</template>
<style>
.asd {
color: purple;
}
</style>
In the result, the text is not purple.
I have checked if it is overridden by some other css but no, it does not event show up in the styles screen of dev tools:
What I have tried and checked:
- I have tried writing style as "p.asd {..." and adding an
!important
in the end. No change. - I have checked if I am working on the correct page by changing the text from "Hello" to "Hello World" and it changes, so I am working on the correct page.
- I got a production build if it is a dev env issue, but no, got the same result from the built html.
- I have tried moving CSS to
App.vue
instead, and surprisingly styling written there works but not in the views. - I have tried on a new browser for caching issues, no change.
- I have tried adding scoped, no change.
- I have tried adding script tags, no change. Script tags work fine, console.log script works, but still no styling update.
- I have tried inline CSS and it works.
Here is my package.json:
"name": "new-project",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
"format": "prettier --write src/"
},
"dependencies": {
"express": "^4.18.2",
"firebase": "^9.22.1",
"pinia": "^2.0.36",
"vue": "^3.3.2",
"vue-router": "^4.2.0",
"vue-tel-input": "^8.1.1"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/eslint-config-prettier": "^7.1.0",
"eslint": "^8.39.0",
"eslint-plugin-vue": "^9.11.0",
"prettier": "^2.8.8",
"vite": "^4.3.5",
"vite-plugin-webfont-dl": "^3.7.4"
}
}
codesandbox example: https://codesandbox.io/p/sandbox/github/ihsanser/vue-vite-style-bug/tree/master
I don't know if I am missing something very simple, but I could not figure out the reason for hours now.
答案1
得分: 1
.asd {
中使用的空格字符实际上不是一个真正的空格。它是一个NBSP <sup>1</sup>。
将其替换为一个真正的空格,一切将按预期工作。这与Vue无关,行为在React、Angular、Svelte或vanilla中都将相同。
另外,这显示了可运行的 mcve有多重要。在你的问题中,你发布了带有空格的代码。不确定这是因为浏览器在呈现当前页面的HTML时对NBSP进行了> Space的转换,还是你只是没有复制/粘贴相关的代码。
我怀疑是第一种情况。
<sup>1</sup> - 编码NBSP输出%C2%A0
,而编码普通空格输出%20
。
英文:
The space character used in .asd {
is not an actual space. It is a NBSP <sup>1</sup>.
Replace it with an actual space and everything will work as expected. This is not related to Vue, the behavior would be the same in React, Angular, Svelte or vanilla.
As a side-note, this shows how important a runnable mcve is. In your question, you posted the code with a space. Not sure if this is due to the NBSP > Space conversion made by the browser when rendering the current page's HTML or if you simply didn't copy/paste the code in question.
I suspect it's the first.
<sup>1</sup> - encoding NBSP outputs %C2%A0
, whereas encoding a normal space outputs %20
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论