javascript - 引用圖片路勁問題
問題描述
圖片的引用:
<img src='http://m.4tl426be.cn/assets/image/setting.png'/>
目錄結(jié)構(gòu):
寫的相對路勁,為什么圖片沒有顯示出來?console提示:
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'),
路勁應(yīng)該寫相對于dist的路徑但是現(xiàn)在又有一個問題!這樣寫在生產(chǎn)環(huán)境肯定是顯示不了圖片的!我不打算將靜態(tài)資源放在/dist下,樓下說了static文件的方法,這個應(yīng)該要配置webpack,有沒有朋友分享一下
問題解答
回答1:靜態(tài)資源的問題已解決,解決方案:使用webpack的一個插件,生產(chǎn)環(huán)境編譯時可以將/static目錄的文件拷貝到/dist目錄下
new CopyWebpackPlugin([ {from: path.resolve(__dirname, ’static’),to: path.resolve(__dirname, ’dist/static’),ignore: [’*.js’] }])
開發(fā)的時候的時候webpack-devserver配置:
contentBase: path.resolve(__dirname),publicPath: '/',
這樣在代碼里面只要寫絕對路徑/static/..就行了
回答2:你寫img的文件在哪里啊? '../assets/image/setting.png'這個只有你寫img的文件在components目錄下才有效.
回答3:如果是使用vue-cli的webpack模板,會被轉(zhuǎn)成絕對路徑,如:/images/setting.png,建議將圖片放在static文件夾下。
回答4:固定路徑都放在/static下面把,唯一打包前打包后位置不變的地方
相關(guān)文章:
1. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)2. 關(guān)于mysql聯(lián)合查詢一對多的顯示結(jié)果問題3. python中如何計算t分布的值?4. mysql在限制條件下篩選某列數(shù)據(jù)相同的值5. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。6. python執(zhí)行cmd命令,怎么讓他執(zhí)行類似Ctrl+C效果將其結(jié)束命令?7. python - scrapy url去重8. 實現(xiàn)bing搜索工具urlAPI提交9. python - Django有哪些成功項目?10. Python從URL中提取域名
