是否可以使用ES6模块导入.css文件? [英] Is it possible to import a .css file using ES6 modules?

查看:1464
本文介绍了是否可以使用ES6模块导入.css文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to import a .css file using ES6 modules but I am getting an error. Is it even possible to import a css file without using a bundler or a transpiler (like webpack)?

无法加载模块脚本:服务器回应为 非JavaScript MIME类型的文本/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:

You can't do it with ES6 modules. You can dynamically import it into the HTML though:

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

这篇关于是否可以使用ES6模块导入.css文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆