Vla*_*col 5 django django-templates django-staticfiles ecmascript-6 es6-modules
我想在 Django 的 index.html 中使用 ES6 中的导入功能。
我不想编译到 ES5 以实现浏览器兼容性。我想假设所有用户都拥有兼容 ES6 的浏览器。
因此我不需要像 Babel 这样的 ES6 到 ES5 编译器:https : //github.com/kottenator/django-compressor-toolkit
如果可以的话,我只想提供 ES6 Javascript 和浏览器来编译它。
我试过:
<!-- Index.html -->
<script type="module" src="{% static 'app.js' %}"></script>
Run Code Online (Sandbox Code Playgroud)
//app.js
(function(){
console.log("Hello from App.js");
})();
Run Code Online (Sandbox Code Playgroud)
# settings.py
if DEBUG:
import mimetypes
mimetypes.add_type("module", ".js", True)
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。每个 HTML 规范对模块脚本执行严格的 MIME 类型检查。
更新 1:我试过:
<script type="module" src="{% static 'app.js' %}"></script>
<script nomodule src="{% static 'app.js' %}"></script>
<script type="module">import "{% static 'main.mjs' %}";</script>
Run Code Online (Sandbox Code Playgroud)
https://developers.google.com/web/fundamentals/primers/modules
更新 2:是否可以不使用预编译器?
https://github.com/kottenator/django-compressor-toolkit
更新 3:我发现
https://module-script-tests-sreyfhwvpq.now.sh/mime
我使用 Chrome: Google Chrome 是最新版本 71.0.3578.98(官方版本)(64 位)
更新 4: 我想使用
<!-- index.html -->
<script type="module"> instead of <script type="text/javascript">
Run Code Online (Sandbox Code Playgroud)
因为我希望能够导入模块:
<!-- index.html -->
<script type="application/javascript" src="{% static 'app.js' %}"></script>
//app.js
import { print } from "{% static 'component.js' %}"
Run Code Online (Sandbox Code Playgroud)
目前给出错误:
未捕获的语法错误:意外的标记 {
更新 5:
这是我的文件结构:
这是我的索引:
{% extends 'base.html' %}
{% block content %}
{% load static from staticfiles %}
<h1>Index</h1>
{% include 'component.html' %}
<script type="module" src="{% static 'app.js' %}"></script>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
这是我的 base.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Index</title>
{% load static from staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的 app.js:
import {A} from "./component.js"
console.log("A" + A)
Run Code Online (Sandbox Code Playgroud)
这是我的 component.js:
export const A = 42
Run Code Online (Sandbox Code Playgroud)
这是我仍然得到的错误:
加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。每个 HTML 规范对模块脚本执行严格的 MIME 类型检查。
这是我得到的内容类型:
我终于让它工作了。
这就是我需要添加到设置中的内容。
if DEBUG:
import mimetypes
mimetypes.add_type("application/javascript", ".js", True)
Run Code Online (Sandbox Code Playgroud)
我犯的错误是在调试时刷新但没有清除缓存。
如果您调试网络内容,请务必在测试时按 ctrl+F5。
如果我评论 mimetype:
if DEBUG:
import mimetypes
#mimetypes.add_type("application/javascript", ".js", True)
Run Code Online (Sandbox Code Playgroud)
然后我得到错误:
无法加载模块脚本:服务器以非 JavaScript MIME 类型“text/plain”进行响应。根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。
我看到这个:
| 归档时间: |
|
| 查看次数: |
3955 次 |
| 最近记录: |