繁体   English   中英

由于不允许的 MIME 类型 (“”),加载模块被阻止

[英]Loading module from was blocked because of a disallowed MIME type (“”)

我正在查看 three.js 示例,当我在 javascript 模块中使用import时,我收到错误消息: Loading module from “http://localhost:8000/build/three.module.js” was blocked because of a disallowed MIME type (“”).

当我传统上使用脚本标签导入时,我没有收到此错误。 这是我的代码:

注释掉的行将呈现一个旋转的立方体

<!DOCTYPE html>
<html>
    <head>
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        <script type="module">

            import * as THREE from '/build/three.module.js';
            // const scene = new THREE.Scene();
            // const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

            // const renderer = new THREE.WebGLRenderer();
            // renderer.setSize( window.innerWidth, window.innerHeight );
            // document.body.appendChild( renderer.domElement );

            // const geometry = new THREE.BoxGeometry();
            // const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
            // const cube = new THREE.Mesh( geometry, material );
            // scene.add( cube );

            // camera.position.z = 5;

            // const animate = function () {
            //  requestAnimationFrame( animate );

            //  cube.rotation.x += 0.01;
            //  cube.rotation.y += 0.01;

            //  renderer.render( scene, camera );
            // };

            // animate();
        </script>
    </body>
</html>

相比之下,这很好用:

<!DOCTYPE html>
<html>
    <head>
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        <script src="build/three.js"></script>
        <script>
        // ...
        </script>
    </body>
</html>

我的文件结构是:

|_index.html
|_ ...
|_build/
    |_ three.module.js
    |_ three.js
    |_ ...

为什么在模块中使用import时会出现 MIME 类型错误? 我希望能够使用这种导入方法,因为所有其他 three.js 示例似乎都在 JS 模块中执行此操作。

我收到相同的错误消息Loading module from “http://localhost:8080/build/three.module.js” was blocked because of a disallowed MIME type (“”). 当我在three_js/examples/文件夹中启动我的 http 服务器时。 three_js/中启动 http-server 可以解决问题。 原因是我的导入语句是import * as THREE from '../build/three.module.js'; . 我不确定这能解决你的确切问题,但它似乎密切相关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM