vue+Springboot项目报:Not allowed to load local resource

很久没写vue项目,再次写报了这个错误
其实就是浏览器的保护,不让直接获取本地磁盘的文件

第一步:后端创建配置类,实现 WebMvcConfigurer 接口

package Router.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class MyConfigurer implements WebMvcConfigurer {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/image/**").addResourceLocations("file:D:/img/");
  }
}

第一个 addResourceHandler 方法里面填写你想要设置的虚拟路径,下面
addResourceLocations 方法填写资源的绝对路径。配置完成后,虚拟路径为
http://localhost:配置类端口号/doctor/图片名称。

第二步:前端配置路径

 data() {
        return {
            rodeList:[],
            radio:false,
            checked:[],
            typeList:[],
            url:"http://localhost:8080/image/"
        }
    },
    ```

<td><img  :src="url+router.position" alt="" >

猜你喜欢

转载自blog.csdn.net/weixin_48143996/article/details/128833294