Vue項目靜態(tài)文件之圖片的引用
一、前提
工程文件目錄
?|- mock
?|- node_modules
?|- public
?|- src
??|-- api
??|-- assets
??|-- components
??|-- router
??|-- store
??|-- style
????|-- test1.scss
????|-- test1_main.scss
??|-- utils
??|-- views
????|-- static_test
??????|-- test1.scss
??|-- App.vue
??|-- main.js
二、背景圖的引用
背景圖樣式直接寫在element的style屬性里
直接在html模板里胰挑,在dom對象的style里加背景圖满着,不起效果召廷,代碼如下:
<div style="height:100px; background:url('../../assets/img/bg.png') center top no-repeat">
不起效果矿卑。柬焕。窝剖。
</div>
背景樣式class寫在vue文件的style標簽里
寫法:
background: url('圖片相對當前的路徑') center top no-repeat
<el-card class="box-card sudoku_item">
<div slot="header" class="clearfix">
<span>背景圖寫在css里</span>
</div>
<div class='testBackground'>
起效果麻掸。。枯芬。
</div>
</el-card>
<style lang="scss" scoped>
.testBackground {
height: 100px;
background: url('../../assets/img/bg.png') center top no-repeat
}
</style>
編譯后的css:
.testBackground[data-v-eb2b930e] {
height: 100px;
background: url(../img/bg.187d48cd.png) top no-repeat;
}
背景樣式class寫在樣式文件里论笔,在vue文件里引入樣式文件
樣式寫法:
background: url('圖片相對當前vue文件的路徑') center top no-repeat
這個圖片路徑很重要,url是圖片相對于當前vue文件的路徑千所,而不是圖片相對于樣式文件的路徑狂魔。
html寫法, /views/static_test/test1.vue:
<el-card class="box-card sudoku_item">
<div slot="header" class="clearfix">
<span>背景圖寫在style file里</span>
</div>
<div class='testBackground_file'>
起效果淫痰。最楷。。
</div>
</el-card>
樣式寫法待错, /style/test1.scss:
.testBackground_file {
height: 100px;
background: url('../../assets/img/bg.png') center top no-repeat
}
/views/static_test/test1.vue籽孙,引入樣式文件寫法:
<style lang="scss" scoped>
@import "../../style/test1.scss";
</style>
編譯后:
.testBackground_file[data-v-eb2b930e] {
height: 100px;
background: url(../img/bg.187d48cd.png) top no-repeat;
}
背景樣式class寫在樣式文件里,在main.js里引入樣式文件
html寫法火俄, /views/static_test/test1.vue:
<el-card class="box-card sudoku_item">
<div slot="header" class="clearfix">
<span>背景圖寫在style file里</span>
</div>
<div class='testBackground_file_main'>
起效果犯建。。瓜客。
</div>
</el-card>
樣式寫法, /style/test1_main.scss:
.testBackground_file_main {
height: 100px;
background: url('../assets/img/bg.png') center top no-repeat
}
請注意适瓦,url是main.js相對于圖片文件的路徑竿开。
引入樣式文件方法,main.js:
// style
import './style/test1_main.scss'
引進來的樣式做為系統(tǒng)公共樣式玻熙。
編譯后:
.testBackground_file_main {
height: 100px;
background: url(../img/bg.187d48cd.png) top no-repeat;
}
三否彩、圖片的顯示
圖片引入直接寫在html里
html寫法:
<img src='../../assets/img/dog.jpg'>
編譯后:
<img data-v-eb2b930e="" src="/img/dog.146655c9.jpg">
圖片url在js的data里定義
html寫法:
<span><img :src='imgUrl'>直接寫url是不起效果的</span>
<span><img :src='imgUrl_require'>需要用require</span>
js寫法:
data () {
return {
imgUrl: '../../assets/img/up.jpg',
imgUrl_require: require('../../assets/img/up.jpg')
};
},
直接定義路徑鏈接是不起效果的,需要在鏈接外面加上require嗦随。
編譯后:
<img data-v-eb2b930e="" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwMDAwQDAwQFBAMEBQcFBAQFBwgGBgcGBggKCAgICAgICggKCgsKCggNDQ4ODQ0SEhISEhQUFBQUFBQUFBT/2wBDAQUFBQgHCA8KCg8SDwwPEhYVFRUVFhYUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAAgABwDAREAAhEBAxEB/8QAGwAAAQQDAAAAAAAAAAAAAAAACAADBgcBAgX/xAApEAABAwMDAwMFAQAAAAAAAAABAgMFAAQGERIhBxMxCEFhIiMyQnGR/8QAGgEAAgMBAQAAAAAAAAAAAAAAAQQABQYCA//EAB8RAAIDAAIDAQEAAAAAAAAAAAABAgMRBAUSITETYf/aAAwDAQACEQMRAD8APvXioQqPqL19xrApNmJ7IkrtSgLgId2dpJ99QhzWkLuwhXLCr5Pawol4/Sw8YyeJyyJYl4d5L1s+kK0B5Tr7EeRTVdiktQ9TdG2Oo7Wtep7YUd6geq8hgtkiDhG3BLyDAWi5Gv20rK0gpI/bVuq7nchx9IqO05UqliA7yWAyO1et5DIAtN1KK3IL3Czv5Cuf7WetrlusytsJv3L6yf4NlWadF5qzF2lxcJJaLUxz2nEk7d6fGpHtTnGsnS1vxjvGus40l/Q5YSTRNQ0fMMpUhq/tWrpCFfkEvICwD8jdWjUtRsK5bHRqXxqDnHGXZWxYunGDq2p1CVkeeOQeOaFlMZnM6IWe2Cj6pbItZXDFpsJttzaG9vHgJGnxVNz6n5LDO9vB/pHxQRWL4vDS+IwgmbBi5fYZSUF1CVlP1a8Eg8VZU0xlBaXVFMJVrSbNMtW7TbDKQ2y2kIbQngBKeAAKbSwdWR9DtQJqUA+R/tBpMDWi0NEJnbUOfE//2Q==">
圖片url由js動態(tài)設置
html寫法:
<el-card class="box-card sudoku_item">
<div slot="header" class="clearfix">
<span>圖片url由js動態(tài)設置</span>
<el-button style="float: right; padding: 3px 0" type="text" @click="setImageUrl1">down</el-button>
<el-button style="float: right; padding: 3px 0" type="text" @click="setImageUrl2">up</el-button>
</div>
<div class='testBackground_file'>
<img :src='imgUrl2'>
</div>
</el-card>
js寫法:
data () {
return {
imgUrl2: require('../../assets/img/up.jpg')
};
},
methods: {
setImageUrl1 () {
this.imgUrl2 = require('../../assets/img/down.jpg')
},
setImageUrl2 () {
this.imgUrl2 = require('../../assets/img/up.jpg')
}
}
在js定義的路徑都需要加上require列荔。
編譯后:
<img data-v-eb2b930e="" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwMDAwQDAwQFBAMEBQcFBAQFBwgGBgcGBggKCAgICAgICggKCgsKCggNDQ4ODQ0SEhISEhQUFBQUFBQUFBT/2wBDAQUFBQgHCA8KCg8SDwwPEhYVFRUVFhYUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAAfAB4DAREAAhEBAxEB/8QAGwAAAQQDAAAAAAAAAAAAAAAACAEFBgcAAgP/xAArEAABAwQBAgMJAQAAAAAAAAABAgMRAAQFBgcSQRMhMxUiIzE1QmGBgpH/xAAbAQACAgMBAAAAAAAAAAAAAAAABQMEAgYHAf/EACURAAEDAwMDBQAAAAAAAAAAAAEAAwQCERIFBhMhMTIiQVFScf/aAAwDAQACEQMRAD8APuf8oQq55d3vLaNh0XuHQwu5WoD46StMT+FJqpMfLVNwtl23o7eoO4uXH4nPjPbL7cNaYyuSbaRdrA8TwgUokifIEqrOM7yUXVTXdNohSMKOymYqwEluq95gvdqx+sm71b1W1FVyQJUG+n7R386qy6qhT0WwbbajOSMXvdDntfJl1tmrt4rMA+1bdQClxHVCqTOyc6MSuoaZoNMORyNeBXfDcpZXDaxa61rYUMgrpC3QJM/LpAjvWTcoijGlRTdvNuyC+94onuP3s8/rVq7sf1FY6v4IETTtonEE91yjWKGRIIa8QpO40h1BbcSFIUIKamNillJxNwh35X4RceeVmtTt5WtUvWbae57gAUolwvquk7c3aKBxSD0+U98R8NM4RtvN7CyF5M+8yy4PT/RgzUsSEKBc91Q3JugyDxNH0K70p6RApitEJuto869QkihCyKEJaEL/2Q==">
四、打包后生成的圖片路徑分析
打包后生成的文件目錄
??不管發(fā)布到根目錄枚尼,還是發(fā)布到子目錄贴浙,打包后生成的文件層次結構是一樣的,如下:
?|- dist
??|-- css
??|-- fonts
??|-- img
??|-- js
??|-- favicon.ico
??|-- index.html
打包后引用圖片的路徑
??編譯后所有非require引入的圖片都被編譯到/dist/img/目錄下面姑原,并且重命名悬而,引用路徑亦已改變,路徑隨圖片文件位置和引用圖片的文件的位置而調整了锭汛。
??而require引入的圖片則生成了圖片base64編碼笨奠,圖片路徑就是圖片base64編碼。
1唤殴、背景圖路徑
???編譯打包后生成的背景圖路徑格式為:?../img/##.png般婆,
???編譯打包后所有的樣式代碼在css目錄的文件中,引用圖片的文件是樣式文件朵逝,引用圖片的路徑是樣式文件相對于圖片位置的路徑蔚袍,所以由'../img'開頭。
2配名、圖片路徑
???編譯后的<img>引用路徑格式是:?/img/##.png啤咽,和背景圖的引用路徑不一樣。
???引用圖片的文件是html渠脉,html文件在根目錄宇整,所以引用路徑由'/img'開頭。
3芋膘、圖片base64編碼
???使用require引入的圖片鳞青,經編譯后直接生成了圖片base64編碼。
打包后生成的index.html文件
發(fā)布到根目錄的情況
.env.production为朋,環(huán)境變量配置
VUE_APP_CURRENTMODE = 'production'
outputDir = 'dist'
vue.config.js配置
outputDir: process.env.outputDir,
assetsDir: '',
publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
生成的index.html
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1">
<link rel=icon href=/favicon.ico>
<title>vue-simple</title>
<link rel=stylesheet href=https://unpkg.com/leaflet@1.3.1/dist/leaflet.css>
<script src=https://unpkg.com/leaflet@1.3.1/dist/leaflet.js></script>
<link href=/css/chunk-1f9b94c5.d8e73c38.css rel=prefetch>
<link href=/css/chunk-3c9c611f.7ff185d6.css rel=prefetch>
<link href=/css/chunk-56343cd2.d37b5fd0.css rel=prefetch>
<link href=/js/chunk-1f9b94c5.96c7cd36.js rel=prefetch>
<link href=/js/chunk-3c9c611f.3070ee4d.js rel=prefetch>
<link href=/js/chunk-56343cd2.722cd6e6.js rel=prefetch>
<link href=/css/app.9e145aa4.css rel=preload as=style>
<link href=/css/chunk-vendors.c5637d35.css rel=preload as=style>
<link href=/js/app.d24b3ba9.js rel=preload as=script>
<link href=/js/chunk-vendors.9e634535.js rel=preload as=script>
<link href=/css/chunk-vendors.c5637d35.css rel=stylesheet>
<link href=/css/app.9e145aa4.css rel=stylesheet>
</head>
<body>
<noscript>
<strong>We're sorry but vue-simple doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id=app></div>
<script src=/js/chunk-vendors.9e634535.js></script>
<script src=/js/app.d24b3ba9.js></script>
</body>
</html>
發(fā)布到子目錄的情況
.env.production臂拓,環(huán)境變量配置
VUE_APP_CURRENTMODE = 'production'
outputDir = 'dist'
vue.config.js配置
outputDir: process.env.outputDir,
assetsDir: '',
publicPath: process.env.NODE_ENV === 'production' ? '/vue-demo' : '/',
生成的index.html
<!DOCTYPE html><html lang=en>
<head><meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1">
<link rel=icon href=/vue-demo/favicon.ico>
<title>vue-simple</title>
<link rel=stylesheet href=https://unpkg.com/leaflet@1.3.1/dist/leaflet.css>
<script src=https://unpkg.com/leaflet@1.3.1/dist/leaflet.js></script>
<link href=/vue-demo/css/chunk-1f9b94c5.d8e73c38.css rel=prefetch>
<link href=/vue-demo/css/chunk-3c9c611f.7ff185d6.css rel=prefetch>
<link href=/vue-demo/css/chunk-56343cd2.d37b5fd0.css rel=prefetch>
<link href=/vue-demo/js/chunk-1f9b94c5.96c7cd36.js rel=prefetch>
<link href=/vue-demo/js/chunk-3c9c611f.3070ee4d.js rel=prefetch>
<link href=/vue-demo/js/chunk-56343cd2.722cd6e6.js rel=prefetch>
<link href=/vue-demo/css/app.9e145aa4.css rel=preload as=style>
<link href=/vue-demo/css/chunk-vendors.c5637d35.css rel=preload as=style>
<link href=/vue-demo/js/app.1da8b7e5.js rel=preload as=script>
<link href=/vue-demo/js/chunk-vendors.9e634535.js rel=preload as=script>
<link href=/vue-demo/css/chunk-vendors.c5637d35.css rel=stylesheet>
<link href=/vue-demo/css/app.9e145aa4.css rel=stylesheet>
</head>
<body>
<noscript>
<strong>We're sorry but vue-simple doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id=app></div>
<script src=/vue-demo/js/chunk-vendors.9e634535.js></script>
<script src=/vue-demo/js/app.1da8b7e5.js></script>
</body>
</html>