首页 > 解决方案 > 用于 JS 和 JSON 文件/mimetype 扩展的 Python Flask ES6 导入

问题描述

我想对 JS/JSON 文件使用 ES6 导出/导入

    // ui.js
    import sampleData from '../data/sample-data.json';
    
    export function initUI() {
      console.log(sampleData)
    }

我有一个烧瓶应用程序并在main.py

我添加了以下内容(我也尝试过切换顺序)。


    mimetypes.add_type('application/json', ".json", strict=True)
    mimetypes.add_type('application/javascript', ".js", strict=True)

这仅适用于 JS 文件。对于 JSON 导入,我收到以下错误。

Failed to load module script: The server responded with a non-JavaScript MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec.

我的想法是因为json有前缀js。因此,所有json导入都被视为.js文件。

我怎样才能解决这个问题?

标签: javascriptpythonjsonflaskes6-modules

解决方案


推荐阅读