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

相关推荐
山峰哥1 小时前
SQL调优实战:从索引失效到性能飙升的破局之道
服务器·数据库·sql·性能优化·编辑器·深度优先
山峰哥13 小时前
数据库调优实战:索引策略与查询优化案例解析
服务器·数据库·sql·性能优化·编辑器
念越20 小时前
VS2022安装与第一个C程序编写教程
c++·vscode
文艺倾年1 天前
【源码精讲+简历包装】LeetcodeRunner—手搓调试器轮子(20W字-上)
java·jvm·人工智能·tomcat·编辑器·guava
加成BUFF1 天前
基于DeepSeek+Python开发软件并打包为exe(VSCode+Anaconda Prompt实操)
vscode·python·prompt·conda·anaconda
SiYuanFeng1 天前
vscode中markdown使用单个$显示公式($...$),preview页面里公式显示失败
ide·vscode·编辑器
御坂10101号1 天前
Total Recall:让 Claude 学会忘记,优化记忆管理
经验分享·vscode·开源·软件工程·ai编程
Rsingstarzengjx1 天前
LaTeX公式编辑语法
编辑器·latex语法
嵌入小生0071 天前
线程 --- 嵌入式(Linux)
linux·vscode·vim·嵌入式·线程·进程
coderYYY2 天前
VSCode终端启动报错
前端·ide·vscode·npm·编辑器