遇到的問題:javascript
This dependency was not found
;dev
好好的,build
以後涼涼,圖片加載404
//webpack.base.conf.js alias: { '@': resolve('src'), //加入 'assets': resolve('src/assets') }
template
可以使用@
、~
style
只能使用~
script
只能使用@
static
文件寫相對路徑根據limit:10000
,使用兩張圖片:css
<template> <div> <div> img+src:@ <img src="@/assets/images/jiaban.jpg" height="200px"> <img src="@/assets/images/cat.png" alt=""> </div> <div> img+src:~ <img src="~assets/images/jiaban.jpg" height="200px"> <img src="~assets/images/cat.png" alt=""> </div> <div> img+js(attrs): <img :src="jiaban" height="200px"> <img :src="cat" alt=""> <ul> <li>{{jiaban}}</li> <li>{{cat}}</li> </ul> </div> <div class="css-bg"> img+css(background-images): <span class="css-bg__1"></span> <span class="css-bg__2"></span> </div> <div> static: <img src="static/images/jiaban.jpg" height="200px"> <img src="static/images/cat.png" alt=""> </div> </div> </template> <script> const jiaban = require('@/assets/images/jiaban.jpg'); const cat = require('@/assets/images/cat.png'); export default { data(){ return { jiaban, cat } } } </script> <style lang="scss"> .css-bg__1, .css-bg__2{ display: inline-block; } .css-bg__1{ height: 200px; width: 173px; background-image: url(~assets/images/jiaban.jpg); background-size: contain; } .css-bg__2{ height: 49px; width: 49px; background-image: url(~assets/images/cat.png); background-size: contain; } </style>
開發環境截圖:
html
添加ExtractTextPlugin
中publicPath
配置,這裏根據實際狀況配:vue
//build/util.js // Extract CSS when that option is specified // (which is the case during production build) if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: 'vue-style-loader', publicPath: '../../', }) } else { return ['vue-style-loader'].concat(loaders) }
生產環境截圖:
java
https://vue-loader.vuejs.org/zh/
https://github.com/vuejs/vue-loader/issues/193
https://segmentfault.com/q/1010000004582219webpack