vue3 vite打包之后访问不显示页面

1 篇文章 0 订阅
1 篇文章 0 订阅

报错内容

index-DT1kpfkt.js:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

解决办法:

1、安装 @vitejs/plugin-legacy -D

npm i @vitejs/plugin-legacy -D

2、vite.config.js 添加配置

import legacy from '@vitejs/plugin-legacy';
export default defineConfig({
  plugins: [
    legacy({
      targets: ['defaults', 'not IE 11']
    }),
    vue(),
  ],
  base:'./', // https://cn.vitejs.dev/config/shared-options.html#base
})

3、将路由模式换成 hash 路由

import { createRouter, createWebHashHistory } from 'vue-router'

再次打包访问,如果还是不行的话就在打包完成后的 index.html 文件的末尾加上以下代码

<script>
  (function (win) {
    let src = document.getElementsByTagName('script');
    for(let i = 0; i < src.length; i++) {
      let script = src[i];
      let url = script.getAttribute("src");
      let type = script.getAttribute("type");
      let scriptText = script.innerHTML;
      if (url || type === "module") {
        let tag=document.createElement('script');
        tag.setAttribute('url',url);
        tag.innerHTML = scriptText;
        script.remove();
        document.getElementsByTagName('head')[0].appendChild(tag);
      }
    }
  })(window)
</script>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值