时间:2021-07-01 10:21:17 帮助过:25人阅读
这里写图片描述
需求:如何components里面的index.vue怎样能把assets里面的图片拿出来。
1.在img标签里面直接写上路径:
- <img src="../assets/a1.png" class="" width="100%"/>
2.利用数组保存再循环输出:
- <el-carousel-item v-for="item in carouselData" :key="item.id">
- <img :src="item.url" class="carouselImg"/>
- <span class="carouselSpan">{{ item.title }}</span>
- </el-carousel-item>
- data: () => ({
- carouselData:[
- {url:require('../assets/a1.png'),title:'你看我叼吗1',id:1},
- {url:require('../assets/a3.png'),title:'你看我叼吗2',id:2},
- {url:require('../assets/a4.png'),title:'你看我叼吗3',id:3}
- ]
- }),
效果如下:
index.vue里面的完整代码是这个:
- <template>
- <p> <p class=" block">
- <el-carousel class="carouselBlock">
- <el-carousel-item v-for="item in carouselData" :key="item.id">
- <img :src="item.url" class="carouselImg"/>
- <span class="carouselSpan">{{ item.title }}</span>
- </el-carousel-item>
- </el-carousel>
- </p>
- <footer1></footer1>
- <img src="../assets/a1.png" class="" width="100%"/>
- </p>
- </template>
- <script>
- import footer1 from '../components/public/footer'
- export default {
- data: () => ({
- carouselData:[
- {url:require('../assets/a1.png'),title:'你看我叼吗1',id:1},
- {url:require('../assets/a3.png'),title:'你看我叼吗2',id:2},
- {url:require('../assets/a4.png'),title:'你看我叼吗3',id:3}
- ]
- }),
- components:{
- footer1
- },
- }
- </script>
- <style lang="scss">
- @import '../style/mixin';
- .carouselBlock{
- width: 100%;
- height: REM(300);
- position:relative;
- .carouselImg{
- height: REM(300);
- width:100%;
- }
- .carouselSpan{
- position: absolute;
- bottom: REM(20);
- left: REM(20);
- font-size: REM(24);
- font-weight: bold;
- }
- }
- .el-carouselcontainer{
- width: 100%;
- height: REM(300);
- }
- .el-carouselitem h3 {
- color: #475669;
- font-size: 14px;
- opacity: 0.75;
- margin: 0;
- }
- .el-carouselitem:nth-child(2n) {
- background-color: #99a9bf;
- }
- .el-carouselitem:nth-child(2n+1) {
- background-color: #d3dce6;
- }
- </style>
PS:下面看下Vue.js中的图片引用路径
当我们在Vue.js项目中引用图片时,关于图片路径有以下几种情形:
使用一
我们在data里面定义好图片路径
imgUrl:'../assets/logo.png'
然后,在template模板里面
- <<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">img src="
- {{imgUrl}}">
这样是错误的写法,我们应该用v-bind绑定图片的srcs属性
:src="imgUrl">
或者
- <span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">img src="../assets/logo.png">
这种方式是按照正常HTML语法引用路径,放在模板里可以被webpack打包出来。
使用二
当我们需要在js代码里面写图片路径的时候,如果我们在data里面写
imgUrl:'../assets/logo.png'
此时webpack只会把它当做字符串处理从而找不到图片地址,此时我们可以使用import引入图片路径:
- :src="avatar" />
- import avatar from '@/assets/logo.png'
在data里面定义
avatar: avatar
情形三
我们也可以把图片放在外层的static文件夹里面,然后在data里面定义:
- imgUrl : '../../static/logo.png'
- :src="imgUrl" />
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
jQuery+koa2怎么实现Ajax请求
iview的select下拉框选项错位怎么处理
以上就是vue的本地静态图片路径的详细内容,更多请关注Gxl网其它相关文章!