css - 防止 vuetify 污染全局样式范围

标签 css vue.js webpack vuetify.js

我试图将一个 Vue 组件从远程源 (npm) 嵌入到另一个组件中,而不使用 Vue 插件。组件按预期安装,但是,因为嵌入式组件使用 Vuetify,它的样式污染了“父”应用程序的样式以下是一些图片,希望能说明我的意思:
在安装嵌入式组件之前注意根应用程序的主要颜色
Style of Root Application Before Mounting Embedded Component
安装登录(嵌入式)组件后:
Style of Root Application After Mounting Embedded Component
我尝试了提到的策略 here ,在嵌入式组件中使用 less 在块级别导入 vuetify css,但它似乎对我不起作用。
我意识到,最终,我可以通过确保嵌入的主题与根应用程序主题匹配来最终解决这个问题,但我宁愿不必依赖它。如果我的组件是使用 webpack 构建的,为什么我无法将 Vuetify 的 css 应用于该组件?有什么建议?
如有必要,很乐意包含代码

最佳答案

要与 vue.js 一起使用,请安装 postcss-parent-selector

npm i postcss-parent-selector -D
然后在 root 中创建一个 postcss.config.js 文件并添加以下代码
module.exports = {
  plugins: [
    require("postcss-parent-selector")({
      selector: ".mywrapperclass",
    }),
  ],
};

然后在 public/index.html 中为应用添加一个包装元素:
<div class="mywrapperclass">
  <div id="app"></div>
</div>
一些链接:
  • https://vue-loader-v14.vuejs.org/en/features/postcss.html
  • https://www.npmjs.com/package/postcss-parent-selector
  • https://github.com/postcss/postcss
  • 关于css - 防止 vuetify 污染全局样式范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64068452/

    相关文章:

    javascript - 维护 Vue 页面之间的状态

    node.js - process.env 从 docker-compose 未定义

    asp.net - 回发 Bootstrap 字体大小更改后(增加)

    html - 如何在 Selenium 定位器中定位 "::after"元素?

    HTML CSS 背景图像保持宽度与浏览器宽度相同

    laravel - npm run watch 不会根据特定更改重新加载

    css - 无法使用 sass-loader (Webpack) 加载背景图像

    jquery - 使按钮不可见(不隐藏)

    javascript - 不能在 Promise 内的 setInterval 中使用它

    javascript - Vue JS,GLTF 加载器错误 "currently no loaders are configured"