vue的本地静态图片路径 vue引入静态图片路径

 2022-10-20    397  

有的朋友可能对于“vue的本地静态图片路径 vue引入静态图片路径”还有很多不明白的地方,下面由77ISP云服务器技术小编为大家讲解一下,下面我们来一起看看吧!

这次给大家带来vue的本地静态图片路径,使用vue本地静态图片路径的注意事项有哪些,下面就是实战案例,一起来看一下。

这里写图片描述

需求:如何components里面的index.vue怎样能把assets里面的图片拿出来。

1.在img标签里面直接写上路径:

2.利用数组保存再循环输出:


    {{ item.title }}

data: () => ({
   carouselData:[
   {url:require('https://www.php.cn/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里面的完整代码是这个:

 

{{ item.title }}

import footer1 from '../components/public/footer' export default { data: () => ({ carouselData:[ {url:require('https://www.php.cn/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 }, } @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; }

PS:下面看下Vue.js中的图片引用路径

当我们在Vue.js项目中引用图片时,关于图片路径有以下几种情形:

使用一

我们在data里面定义好图片路径

imgUrl:’../assets/logo.png’

然后,在template模板里面

img src="
{{imgUrl}}">

这样是错误的写法,我们应该用v-bind绑定图片的srcs属性

:src=”imgUrl”>

或者

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" />

以上就是“vue的本地静态图片路径 vue引入静态图片路径”的详细内容,更多请关注77isp云服务器技术网其它相关文章!

原文链接:https://77isp.com/post/3275.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。