使用React和TypeScript,从静态目录中加载png和svg文件的路径
P粉122932466
P粉122932466 2023-08-30 20:10:18
[React讨论组]

我正在尝试从/static/目录中加载svg和png文件作为路径,以便动态地将它们用作网站图标。

我根据文档配置了所有内容:

./src/model/view/<SomeView.ts>
./static/<SomeFile.png|svg>
./src/custom.d.ts
./tsconfig.json
./webpack.config.js
./package.json

示例视图 BrowserView.ts

import FaviconPng from "../../../static/favicon_browser-32x32.png";
import FaviconSvg from "../../../static/favicon-browser.svg";
import { View } from "./View";

export class BrowserView implements View {
  public readonly faviconPng = FaviconPng;
  public readonly faviconSvg = FaviconSvg;
}

自定义类型声明 custom.d.ts

declare module "*.svg" {
  const path: string;
  export = path;
}

declare module "*.png" {
  const path: string;
  export = path;
}

tsconfig.json

{
  "compilerOptions" : {
    "esModuleInterop" : true,
    "experimentalDecorators" : true,
    "jsx" : "react",
    "moduleResolution" : "node",
    "strict" : true,
    "target" : "ES6"
  },
  "include" : [
    "./src/model/view/*",
    "./src/custom.d.ts"
  ]
}

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|svg)$/,
        use: "url-loader"
      }
    ]
  }
};

package.json中包含了webpackurl-loader

但是我仍然遇到了这个错误:

ERROR in ./static/favicon_browser-32x32.png 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
 @ ./src/model/view/BrowserView.ts 1:0-68 7:26-36
 @ ./src/App.tsx 34:0-69 73:33-51 162:17-40 224:17-40
 @ ./src/index.tsx 4:0-32 5:36-41

ERROR in ./static/favicon_browser.svg 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <?xml version="1.0" encoding="UTF-8"?> <svg id="uuid-00b901b6-ce54-412b-a6b8-3c8e80482087" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 26 32"> <defs> <style>.uuid-80dd75c4-418c-4908-a10c-915b8aaac0d3{ isolation:isolate; fill:black; } @media (prefers-color-scheme: dark) { .uuid-80dd75c4-418c-4908-a10c-915b8aaac0d3{ fill:white; } }</style> </defs> <path class="uuid-80dd75c4-418c-4908-a10c-915b8aaac0d3" d="m7.52,..." /> </svg>
 @ ./src/model/view/BrowserView.ts 2:0-62 8:26-36
 @ ./src/App.tsx 34:0-69 73:33-51 162:17-40 224:17-40
 @ ./src/index.tsx 4:0-32 5:36-41

我猜测它无法识别加载器配置,但我不确定我可以改变什么。

如何从该目录加载图像作为路径来更改网站图标?

private updateFavicon(view: View): void {
    // 通过id标签获取元素
    const favicon_svg: HTMLElement = document.getElementById("favicon_svg")!;
    const favicon_png: HTMLElement = document.getElementById("favicon_png")!;

    // 设置图标
    favicon_svg.setAttribute("href", view.faviconSvg);
    favicon_png.setAttribute("href", view.faviconPng);
  }

P粉122932466
P粉122932466

全部回复(1)
P粉041856955

最好使用相对路径,相对于您的项目 由于未指定webpack版本,很难回答。

这里是webpack 5图片使用的链接 https://webpack.js.org/guides/asset-management/#loading-images

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号