您的位置:首頁技術文章
文章詳情頁

javascript - 引用圖片路勁問題

瀏覽:82日期:2023-05-16 16:32:19

問題描述

圖片的引用:

<img src='http://www.wxshucaidpc.com/assets/image/setting.png'/>

目錄結構:

javascript - 引用圖片路勁問題

寫的相對路勁,為什么圖片沒有顯示出來?console提示:

javascript - 引用圖片路勁問題

webpack:

var path = require('path');var webpack = require('webpack');var HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = { entry: ['react-hot-loader/patch','webpack-dev-server/client?http://0.0.0.0:3000','webpack/hot/only-dev-server','babel-polyfill','whatwg-fetch','./src/index' ], devServer: {hot: true,contentBase: path.resolve(__dirname, 'dist'),port: 3000,host: '0.0.0.0',publicPath: '/',historyApiFallback: true,disableHostCheck: true,proxy: { '/agent': {target: 'http://dn4:19000',changeOrigin: true, }, '/api': {target: 'http://dn4:19989',changeOrigin: true, }, '/sign': {target: 'http://dn4:19000',changeOrigin: true, }, '/file': {target: 'http://dn4:19000',changeOrigin: true, },} }, output: {path: path.join(__dirname, 'dist'),publicPath: '/',filename: 'app.[hash].js' }, devtool: 'eval', module: {rules: [ {test: /.js$/,exclude: /node_modules/,loader: 'babel-loader',options: { presets: [['es2015', {'modules': false}],'stage-0','react' ], plugins: ['transform-async-to-generator','transform-decorators-legacy',['import', {'libraryName': 'antd', 'style': true}] ]} }, {test: /.scss|css$/,use: [ 'style-loader', 'css-loader', 'postcss-loader', 'resolve-url-loader', 'sass-loader?sourceMap'] }, {test: /.less$/,use: [{ loader: 'style-loader' // creates style nodes from JS strings}, { loader: 'css-loader' // translates CSS into CommonJS}, { loader: 'less-loader', options: {modifyVars: { 'primary-color': '#0183ff', 'font-size-base': '16px',} } // compiles Less to CSS}] }, {test: /.(jpe?g|png|gif|svg)$/i,use: [ 'file-loader?hash=sha512&digest=hex&name=[hash].[ext]', {loader: 'image-webpack-loader',options: { progressive: true, optimizationLevel: 7, interlaced: false, pngquant: {quality: '65-90',speed: 4 }} }] }, {test: /.woff(2)?(?v=[0-9].[0-9].[0-9])?$/,use: 'url-loader?limit=10000&mimetype=application/font-woff' }, {test: /.(ttf|eot|svg)(?v=[0-9].[0-9].[0-9])?$/,use: 'file-loader' }] }, plugins: [new webpack.NamedModulesPlugin(),new webpack.HotModuleReplacementPlugin(),new HtmlWebpackPlugin({hash: false, template: './index.hbs'}),new webpack.ContextReplacementPlugin(/moment[/]locale$/, /nb/) ]};

原因找到了:

contentBase: path.resolve(__dirname, 'dist'),

路勁應該寫相對于dist的路徑但是現在又有一個問題!這樣寫在生產環境肯定是顯示不了圖片的!我不打算將靜態資源放在/dist下,樓下說了static文件的方法,這個應該要配置webpack,有沒有朋友分享一下

問題解答

回答1:

靜態資源的問題已解決,解決方案:使用webpack的一個插件,生產環境編譯時可以將/static目錄的文件拷貝到/dist目錄下

new CopyWebpackPlugin([ {from: path.resolve(__dirname, ’static’),to: path.resolve(__dirname, ’dist/static’),ignore: [’*.js’] }])

開發的時候的時候webpack-devserver配置:

contentBase: path.resolve(__dirname),publicPath: '/',

這樣在代碼里面只要寫絕對路徑/static/..就行了

回答2:

你寫img的文件在哪里啊? '../assets/image/setting.png'這個只有你寫img的文件在components目錄下才有效.

回答3:

如果是使用vue-cli的webpack模板,會被轉成絕對路徑,如:/images/setting.png,建議將圖片放在static文件夾下。

回答4:

固定路徑都放在/static下面把,唯一打包前打包后位置不變的地方

標簽: JavaScript
相關文章:
国产综合久久一区二区三区