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 即可

相关推荐
CDialog2 小时前
vscode编译ci1303sdk的源代码
ide·vscode·编辑器
1-1=05 小时前
Visual Studio Code 简介
ide·vscode·编辑器
π同学7 小时前
ESP-IDF+vscode开发ESP32第一讲——GPIO
vscode·esp32·esp-idf
c++之路9 小时前
Vim 完整安装与配置指南(Ubuntu 系统 + Vimplus 增强)
ubuntu·编辑器·vim
HookJames9 小时前
在VSCode中部署OpenRouter的时候,遇到的问题
ide·chrome·vscode
2501_916007479 小时前
提高开发效率的尝试,用快蝎(kxapp)完成 iOS 项目的创建、调试与构建
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
OYangxf10 小时前
vscode在Linux环境下,怎么才能装插件codex以保证不发生token exchange failed
ide·vscode
wjhx10 小时前
vscode中加入Claude Code,对接deepseek
ide·vscode·编辑器
im_AMBER10 小时前
订阅模式实现字符数统计
前端·typescript·前端框架·编辑器
段帅星11 小时前
mac下sublime text优化
macos·编辑器·sublime text