是否可以使用 ES6 模块导入 .css 文件?

Is it possible to import a .css file using ES6 modules?

我正在尝试使用 ES6 模块导入 .css 文件,但出现错误。甚至可以在不使用捆绑器或转译器(如 webpack)的情况下导入 css 文件吗?

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/css"

<html>
    <body>
        <div id="app">
            {{ msg }}
        </div>

        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script type="module">
            import './style.css'

            new Vue({
              el: '#app',
              data: {
                msg: 'Hello World!'
              }
            })
        </script>
    </body>
</html>

你不能用 ES6 模块来做。您 可以 将其动态导入到 HTML 中:

<script>
    const url = "./style.css";
    document.head.innerHTML += `<link type="text/css" rel="stylesheet" href=${url}>`;
</script>