2023年6月21日发(作者:)
在ts中加载css样式和图⽚在 ts 环境中加载 css 样式和图⽚都需要简单的配置,配置流程如下:加载 css 样式⾸先需要下载额外的依赖:npm i --save-dev css-loader style-loader下载完依赖之后,需要到
⽂件中配置识别 css module 的配置项:module: { rules: [ // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. { test: /.tsx?$/, loader: "ts-loader" }, // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. { enforce: "pre", test: /.js$/, loader: "source-map-loader" }, // Load .css files { test: /.css$/, use: ["style-loader", "css-loader"] }, ] },这样就可以在⽂件加载 css 样式了,简单实⽤:)import ""; //
不要忘记了 .css
的后缀
ts 中加载图⽚第⼀步还是下载依赖:npm i --save-dev file-loader下载完依赖之后,接着还是到
⽂件中配置加载图⽚的配置项:接着上⾯ css 的配置项后⾯接着写就ok了module: { rules: [ // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. { test: /.tsx?$/, loader: "ts-loader" }, // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. { enforce: "pre", test: /.js$/, loader: "source-map-loader" }, // Load .css files { test: /.css$/, use: ["style-loader", "css-loader"] },
//Load image { test: /.(jpg|jpeg|png|svg)$/, loader: 'file-loader', options: { name: 'images/[hash].[ext]' } }, ] },最重要的是需要引⼊
⽂件,并且将该⽂件放在
同级路径下, 内容:declare module "*.svg";declare module "*.png";declare module "*.jpg";declare module "*.jpeg";declare module "*.gif";declare module "*.bmp";declare module "*.tiff";这样就可以识别所有不同后缀名的⽂件了引⼊图⽚import img from "../public/images/";
2023年6月21日发(作者:)
在ts中加载css样式和图⽚在 ts 环境中加载 css 样式和图⽚都需要简单的配置,配置流程如下:加载 css 样式⾸先需要下载额外的依赖:npm i --save-dev css-loader style-loader下载完依赖之后,需要到
⽂件中配置识别 css module 的配置项:module: { rules: [ // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. { test: /.tsx?$/, loader: "ts-loader" }, // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. { enforce: "pre", test: /.js$/, loader: "source-map-loader" }, // Load .css files { test: /.css$/, use: ["style-loader", "css-loader"] }, ] },这样就可以在⽂件加载 css 样式了,简单实⽤:)import ""; //
不要忘记了 .css
的后缀
ts 中加载图⽚第⼀步还是下载依赖:npm i --save-dev file-loader下载完依赖之后,接着还是到
⽂件中配置加载图⽚的配置项:接着上⾯ css 的配置项后⾯接着写就ok了module: { rules: [ // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. { test: /.tsx?$/, loader: "ts-loader" }, // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. { enforce: "pre", test: /.js$/, loader: "source-map-loader" }, // Load .css files { test: /.css$/, use: ["style-loader", "css-loader"] },
//Load image { test: /.(jpg|jpeg|png|svg)$/, loader: 'file-loader', options: { name: 'images/[hash].[ext]' } }, ] },最重要的是需要引⼊
⽂件,并且将该⽂件放在
同级路径下, 内容:declare module "*.svg";declare module "*.png";declare module "*.jpg";declare module "*.jpeg";declare module "*.gif";declare module "*.bmp";declare module "*.tiff";这样就可以识别所有不同后缀名的⽂件了引⼊图⽚import img from "../public/images/";
发布评论