解决vue无法解析.ico结尾图片路径

本文介绍了在Vue项目中遇到的ICO图标加载失败的问题及解决方案。通过修改webpack配置文件并重启服务来成功加载ICO格式的图标。
<img src="../assets/img/favicon.ico">

上面路径是对的,可是报错说找不到,why?后来查找资料才发现,vue框架默认配置图片格式没有ico的,所以需要手动加上

在bulid/webpack.base.conf.js中有如下一行:

test: /\.(png|jpe?g|gif|svg|ico)(\?.*)?$/,
上面这是已经加上ico的,但是发现还是报错,难道还有其他问题?

不是的,是因为改了配置后需要退出再启动服务才行。做完这些,应该就ok了。

const path = require('path'); module.exports = { publicPath: process.env.NODE_ENV === 'production' ? '/product-data/' : '/', assetsDir: 'static', configureWebpack: { resolve: { alias: { '@': path.resolve(__dirname, 'src'), '@components': path.resolve(__dirname, 'src/components'), '@views': path.resolve(__dirname, 'src/views'), }, }, plugins: [ new (require('webpack').DefinePlugin)({ __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false, __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false }) ] }, // 开发服务器配置 devServer: { proxy: { // 使用环境变量中的API地址作为代理目标 '/api': { target: process.env.VUE_APP_BASE_API, changeOrigin: true, pathRewrite: { '^/api': '' } } }, client: { overlay: false }, // 新增端口配置,支持环境变量或默认值 port: process.env.VUE_APP_PORT }, // 生产环境配置 productionSourceMap: false, css: { extract: true, }, chainWebpack: config => { config.plugin('html').tap(args => { args[0].title = '产品数据管理系统'; return args; }); } };从提供的 index.html 内容来看,静态资源路径未包含上下文路径 /product-data/,导致浏览器请求时直接访问根路径下的 /static/,从而触发 404 错误。以下是详细的修复方案: 一、问题原因分析 1. 资源路径缺失上下文前缀 当前 index.html 中的资源路径: html 预览 <script defer src="/static/js/chunk-vendors.js"></script> <link href="/static/css/app.765bed1d.css" rel="stylesheet"> 问题:路径以 /static/ 开头,未包含上下文路径 /product-data/,导致浏览器请求 http://localhost:8080/static/...,而实际资源位于 http://localhost:8080/product-data/static/...。 2. Vue 配置中的 publicPath 错误 若 vue.config.js 中 publicPath 未正确设置为 /product-data/,打包后会生成错误的资源路径
06-24
'use strict' const path = require('path') function resolve(dir) { return path.join(__dirname, dir) } const CompressionPlugin = require('compression-webpack-plugin') const name = process.env.VUE_APP_TITLE || '其亚管理系统' // 网页标题 const port = process.env.port || process.env.npm_config_port || 80 // 端口 // vue.config.js 配置说明 //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions // 这里只列一部分,具体配置参考文档 module.exports = { chainWebpack: config => { config.module .rule('tinymce') .test(/tinymce\/.*\.js$/) .use('babel-loader') .loader('babel-loader') .end() }, // 部署生产环境和开发环境下的URL。 // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上 // 例如 https://www.qiya.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.qiya.vip/admin/,则设置 baseUrl 为 /admin/。 publicPath: process.env.NODE_ENV === "production" ? "/" : "/", // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist) outputDir: 'dist', // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下) assetsDir: 'static', // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。 productionSourceMap: false, transpileDependencies: ['quill'], // webpack-dev-server 相关配置 devServer: { host: '0.0.0.0', port: 81, open: true, proxy: { // detail: https://cli.vuejs.org/config/#devserver-proxy [process.env.VUE_APP_BASE_API]: { target: `http://218.31.203.46:8081`, hangeOrigin: true, pathRewrite: { ['^' + process.env.VUE_APP_BASE_API]: '' }, } }, disableHostCheck: true }, css: { loaderOptions: { sass: { sassOptions: { outputStyle: "expanded" } } } }, configureWebpack: { name: name, resolve: { alias: { '@': resolve('src') } }, plugins: [ // http://doc.qiya.vip/qiya-vue/other/faq.html#使用gzip解压缩静态文件 new CompressionPlugin({ cache: false, // 不启用文件缓存 test: /\.(js|css|html|jpe?g|png|gif|svg)?$/i, // 压缩文件格式 filename: '[path][base].gz[query]', // 压缩后的文件名 algorithm: 'gzip', // 使用gzip压缩 minRatio: 0.8, // 压缩比例,小于 80% 的文件不会被压缩 deleteOriginalAssets: false // 压缩后删除原文件 }) ], }, chainWebpack(config) { config.plugins.delete('preload') // TODO: need test config.plugins.delete('prefetch') // TODO: need test // set svg-sprite-loader config.module .rule('svg') .exclude.add(resolve('src/assets/icons')) .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/assets/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() config.when(process.env.NODE_ENV !== 'development', config => { config .plugin('ScriptExtHtmlWebpackPlugin') .after('html') .use('script-ext-html-webpack-plugin', [{ // `runtime` must same as runtimeChunk name. default is `runtime` inline: /runtime\..*\.js$/ }]) .end() config.optimization.splitChunks({ chunks: 'all', cacheGroups: { libs: { name: 'chunk-libs', test: /[\\/]node_modules[\\/]/, priority: 10, chunks: 'initial' // only package third parties that are initially dependent }, elementUI: { name: 'chunk-elementUI', // split elementUI into a single package test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm priority: 20 // the weight needs to be larger than libs and app or it will be packaged into libs or app }, commons: { name: 'chunk-commons', test: resolve('src/components'), // can customize your rules minChunks: 3, // minimum common number priority: 5, reuseExistingChunk: true } } }) config.optimization.runtimeChunk('single') }) } } 这个是我在开发时对前端项目做的代理,现在我要部署到我本地的nginx上,我的nginx.conf配置如下:# 必须的全局配置 worker_processes 1; events { worker_connections 1024; } # 所有 server 块必须包含在 http 块内 http { include mime.types; default_type application/octet-stream; # 日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; error_log logs/error.log; sendfile on; keepalive_timeout 65; # 您的应用配置开始 server { listen 80; server_name 172.26.26.38; # 替换为您的域名或服务器IP root dist_HTGL; # 应该是目录,不是文件 # 启用 Gzip 静态文件支持 gzip_static on; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 路由 history 模式支持 location / { index index.html; try_files $uri $uri/ /index.html; # 缓存配置 if ($request_filename ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) { expires 365d; add_header Cache-Control "public, immutable"; } } # 静态资源目录 location /static/ { alias dist_HTGL/static/; expires max; add_header Cache-Control "public, immutable"; access_log off; } # API 代理配置 location ^~ /process.env.VUE_APP_BASE_API/ { proxy_pass http://218.31.203.46:8081; # 请求头处理 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket 支持 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # 超时设置 proxy_connect_timeout 60s; proxy_read_timeout 600s; proxy_send_timeout 600s; } # 禁止访问敏感文件 location ~ /\.(env|git) { deny all; return 403; } # 错误页面 error_page 404 /index.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 可选:HTTP 重定向到 HTTPS server { listen 80; server_name your-domain.com; return 301 https://$host$request_uri; } } 为什么启动后我登录请求的验证码获取不到
最新发布
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值