VSCode解决本地浏览器需要跨域问题

这里写目录标题

测试用代码

先把测试用的代码贴出来

测试代码结构

index.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0, minimum-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- type="module"意思是直接开启es6模块化 -->
    <script src="./main.js" type="module"></script>
    sss
</body>
</html>

counter.js

javascript 复制代码
export const count = 0;

main.js

javascript 复制代码
import { count } from "./counter.js"

console.log("count");

执行代码后控制台报错现象

Access to script at 'file:///D:/%E5%AD%99%E7%91%9C/%E7%AC%94%E8%AE%B0/%E5%89%8D%E7%AB%AF/vite/vite%E5%AD%A6%E4%B9%A0/test-vite/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

解决方案

给VSCode安装 Live Server 插件

主要作用是在本地开启一个HTTP Server,并且可以监听Ctrl+S动作进行页面自动刷新。即最常见的就是我们在写html时,我们需要在浏览器查看页面布局效果,这时我们可以用Liver Server来打开这页面并再每次保存时刷新页面。

使用时启动页面下方的 Go Live 即可

相关推荐
dyxal6 小时前
VS Code 终端疑难杂症排查:为什么 PowerShell 无法启动?
vscode
【ql君】qlexcel7 小时前
Visual Studio Code开发STM32设置头文件宏定义uint32_t报错
vscode·stm32·vs code·头文件宏定义·uint32_t报错·uint8_t报错·uint16_t报错
琉璃榴7 小时前
Visual Studio Code连接远程服务器
服务器·vscode·github
jieyucx9 小时前
Golang 完整安装与 VSCode 开发环境搭建教程
开发语言·vscode·golang
Codiggerworld12 小时前
Vim的语法:删除、复制、粘贴,像说话一样自然
编辑器·vim·excel
刘延林.12 小时前
Visual Studio Code+PlatformIO + ESP32-S3 + Arduino 框架点亮一个小的led灯-测试
ide·vscode·编辑器
Dontla13 小时前
VSCode插件Git Graph介绍(Git可视化管理分支、可视化Git)
ide·git·vscode
2501_9160074713 小时前
从零开始学习iOS开发:Xcode环境配置与项目创建完整指南
ide·vscode·学习·ios·个人开发·xcode·敏捷流程
π同学15 小时前
ESP-IDF+vscode开发ESP32第六讲——SPI
vscode·esp32·spi
SiYuanFeng15 小时前
打开vscode总是提示未找到python的解决办法(打开终端却能找到)
ide·vscode·python