Vue-loader在运行时从<style>中删除ID选择器

时间:2019-09-24 14:10:57

标签: vue.js webpack vuejs2 webpack-style-loader vue-loader

我想做的是通过我正在使用的library从html生成pdf文件。因此,我需要获取具体的html及其css,然后将css插入html,并将其传递给后端并生成pdf。

我正在使用Vue.js,并且在<style id="myStyle">标记中有一些CSS,并且在运行时尝试通过id访问它,但是问题是,当我启动应用程序时,vue-loader将我的css文件加载到标头中没有#myStyle id选择器。

这是我的示例代码:

<template>
    <div id="app">
        <h3 id="testapp">Test</h3>
    </div>
</template>

<script>

export default {
    name: 'app',
    data() {
        return {

        }
    },
    created() {
        var styleElement = document.getElementById("myStyle"); // it's null, because 
            //there is no element with such selector at rutime
        styleElement.setAttribute("class", "democlass");
    }
}
</script>
 <style id="myStyle">
#app {
    font-family: "Avenir", Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
}
#testapp {
    color: #2c3e50;
    margin-top: 60px;
}
#testing {
    color: #2c3e50;
    margin-top: 60px;
}
</style>

这里是在<head>中加载没有ID的CSS styleTag

有人可以告诉我:

  • 为什么<head><style>中的id选择器在运行时消失了
  • 有没有办法让它停留在那里,所以我可以通过ID选择我的<style>
  • 或者还有其他方法可以在运行时获得具体的<style>

0 个答案:

没有答案