Should I put the scoped CSS in my master file, or should I change the print function in order to accomodate components' scoped CSS? In the second case, how should I change the JS function?

我将Laravel 5与许多Vue组件一起使用.在其中一个例子中,我有以下范围的CSS

<style scoped>
    td.not-purchased{
        background-color: darkgrey;
    }
    td.not-assigned-here{
        background-color: lightgreen;
    }
    td .checkbox{
        margin-top: 0;
        margin-bottom: 0;
        display: inline-block;
    }

    table th:nth-child(n+3),
    table td:nth-child(n+3){
        width: 50px;
        overflow-x: hidden;
        text-overflow: ellipsis;
    }
</style>

打印生成的内容时,该函数会在新页面中打开内容,并将外部CSS复制到原始文档的头部.

$(document).on('click', '.glyphicon[data-printselector]', function(){
        if($(this).data('printselector') != ''){
            // Print in new window
            let printHtml = $($(this).data('printselector')).clone(),
                printWindow = window.open(),
                waitTimeout;

            $(printWindow.document.body).append($('<div />', {class: 'container'}).append(printHtml));


            $.each($('link'), function() {
                $(printWindow.document.head).append($(this).clone());

                clearTimeout(waitTimeout);
                waitTimeout = setTimeout(function(){
                    // Here we ensure that CSS is loaded properly
                    printWindow.print();
                    printWindow.close();
                }, window.changeFunctionTimeoutLong);
            });

        }
        else
            window.print();
    });

我知道这可以通过将作用域CSS直接放入我网站的主CSS中来实现,但我相信这违背了使用作用域CSS的全部意义.

推荐答案

默认情况下,Vue的工作方式是通过以下方式转换范围内的CSS:

  • CSS中的CSS Select 器会获得一个额外的属性 Select 器,CSS本身会以<style>标签的形式内联到页面中
  • 与CSS中的 Select 器匹配的HTML元素被赋予相应的唯一属性

这样,CSS规则的范围就限定为与唯一属性匹配的特定HTML元素.

如果您希望您的范围样式在与原始页面不同的上下文中工作,那么有两件事需要确保工作正常:

  • HTML元素具有必要的属性
  • 带有这些属性的CSS的<style>标签就出现了

从你的描述来看,至少后者不见了.一个快速try 的方法是,在复制外部CSS时,从页面中复制所有<style>个标记,看看是否有效.

然后你可以决定这对你来说是否足够好,或者你是否真的想看看你需要什么样式(如果你有很多样式的话).

Vue.js相关问答推荐

VueJS使所有组件崩溃,而不是只有一个

将 html 文件移出 dist vite 中的 src 文件夹

Vue Router中.getRoutes()获取的路由顺序优化建议

如何在 vue2.7 中删除 slot-scope

如何从 Vuex 状态使用 Vue Router?

如何使用 v-on:change 将输入文本传递给我的 vue 方法?

Vue.js 中有没有类似Jquery $(document).ready的功能?

如何在异步功能上使用 debounce?

VueJS:向左,元素的顶部位置?

Laravel Vue - 如何让 vue 在公共文件夹中查找图像

如何在 vue 3 脚本设置中的组件上使用 v-model

Vue-test-utils:在单个测试中多次使用 $nextTick

使用 Blade/Mustache 模板混合 Laravel 和 Vue.Js

Vue如何将动态ID与v-for循环+字符串中的字段连接起来?

如何在 vue.js 中进行嵌入?

如何从浏览器的源中删除 webpack://

v-for 中的计算(Computed)/动态(Dynamic) v-model 名称

Vue过滤器应该如何使用typescript绑定?

Vue - 重用方法的最佳方式

如何删除 vue cli 2?