<div><img src="/BookShopSystem_war/"+{{hot.image1}}></div>该访问图片路径的拼接正确吗,若错误,如何修改
时间: 2024-02-22 14:56:44 浏览: 34
不正确,应该将字符串和变量拼接在一起,而不是将字符串和表达式拼接在一起。可以使用Vue.js的插值表达式来实现字符串和变量的拼接,例如:
```
<div><img :src="'/BookShopSystem_war/' + hot.image1"></div>
```
其中,:src表示绑定img元素的src属性,使用单引号将字符串'/BookShopSystem_war/'和变量hot.image1拼接在一起。这样就能够正确地访问图片资源了。
相关问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图书商城-图书列表</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="/BookShopSystem_war/layui/css/layui.css"> <link rel="stylesheet" href="/BookShopSystem_war/static/css/index.css"> <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <div class="layui-container"> <div class="hot-sell" v-if="bookAll.length > 0"> <div class="layui-row layui-col-space20"> <table> <tr> <td v-for="(bk,index) in currentBookAll" :key="index" class="layui-col-xs2" style="margin-left: 30px"> <a :href="'detail?id='+bk.bookId" class="text"> <div><img :src="'/BookShopSystem_war/' + bk.image1"></div> <p>{{bk.bookName}}</p> <p class="price">¥{{bk.bookPrice}}</p> </a> </td> </tr> </table> </div> </div> </div> <div id="houseList"></div> </div> <!-- 引入尾部footer.html --> <iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="245px"></iframe> </div> <script src="/BookShopSystem_war/layui/layui.js"></script>这是完整的html代码,请结合你给出的分页功能整合出完整的代码
好的,以下是结合分页功能的完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书商城-图书列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/BookShopSystem_war/layui/css/layui.css">
<link rel="stylesheet" href="/BookShopSystem_war/static/css/index.css">
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<div class="layui-container">
<div class="hot-sell" v-if="bookAll.length > 0">
<div class="layui-row layui-col-space20">
<table>
<tr>
<td v-for="(bk,index) in currentBookAll" :key="index" class="layui-col-xs2" style="margin-left: 30px">
<a :href="'detail?id='+bk.bookId" class="text">
<div><img :src="'/BookShopSystem_war/' + bk.image1"></div>
<p>{{bk.bookName}}</p>
<p class="price">¥{{bk.bookPrice}}</p>
</a>
</td>
</tr>
</table>
</div>
</div>
<div class="layui-row">
<div class="layui-col-md12">
<div id="pagination"></div>
</div>
</div>
</div>
<div id="houseList"></div>
<!-- 引入尾部footer.html -->
<iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="245px"></iframe>
</div>
<script src="/BookShopSystem_war/layui/layui.js"></script>
<script>
layui.use('laypage', function(){
var laypage = layui.laypage;
var vm = new Vue({
el: '#app',
data: {
bookAll: [],
current: 1,
totalPage: 1,
currentBookAll: []
},
mounted: function() {
var _this = this;
axios.get('/BookShopSystem_war/book/all')
.then(function(response) {
_this.bookAll = response.data;
_this.totalPage = Math.ceil(_this.bookAll.length / 10);
_this.currentBookAll = _this.bookAll.slice(0, 10);
laypage.render({
elem: 'pagination',
count: _this.bookAll.length,
curr: _this.current,
limit: 10,
layout: ['prev', 'page', 'next', 'skip'],
jump: function(obj, first){
if(!first){
_this.currentBookAll = _this.bookAll.slice((obj.curr-1)*obj.limit, obj.curr*obj.limit);
}
}
});
})
.catch(function(error) {
console.log(error);
});
}
});
});
</script>
</body>
</html>
注意:这里的分页功能使用了 layui 的 laypage 组件,需要在页面中引入 layui.js 和 layui.css,并且需要在 Vue 的 mounted 钩子函数中调用 laypage.render() 方法来渲染分页组件。同时,需要注意在分页回调函数中更新当前显示的数据。
将该代码重复的部分用数组遍历的方式实现<div class="hot-sell"> <p class="house-title">热销推荐<a href="">更多优品 ></a></p> <div class="layui-row layui-col-space20"> <table> <a href="detail.html" class="layui-col-xs3 text"> <div><img src="/BookShopSystem_war/static/img/home_img4.jpg"></div> <p>一盏好看的台灯</p> <p class="price">¥200</p> </a> <a href="detail.html" class="layui-col-xs3 text"> <div><img src="/BookShopSystem_war/static/img/home_img5.jpg"></div> <p>时尚瓷碗7件套</p> <p class="price">¥200</p> </a> <a href="detail.html" class="layui-col-xs3 text"> <div><img src="/BookShopSystem_war/static/img/home_img6.jpg"></div> <p>智能电吹风</p> <p class="price">¥200</p> </a> <a href="detail.html" class="layui-col-xs3 text"> <div><img src="/BookShopSystem_war/static/img/home_img7.jpg"></div> <p>一盏好看的台灯</p> <p class="price">¥200</p> </a> </table> </div> </div>
你可以将重复的部分用一个包含图片、商品名和价格的对象数组表示,然后用循环语句遍历数组,依次生成HTML代码。示例如下:
```
<div class="hot-sell">
<p class="house-title">热销推荐<a href="">更多优品 ></a></p>
<div class="layui-row layui-col-space20">
<table>
<% var products = [
{ image: '/BookShopSystem_war/static/img/home_img4.jpg', name: '一盏好看的台灯', price: '¥200' },
{ image: '/BookShopSystem_war/static/img/home_img5.jpg', name: '时尚瓷碗7件套', price: '¥200' },
{ image: '/BookShopSystem_war/static/img/home_img6.jpg', name: '智能电吹风', price: '¥200' },
{ image: '/BookShopSystem_war/static/img/home_img7.jpg', name: '一盏好看的台灯', price: '¥200' }
]; %>
<% for (var i = 0; i < products.length; i++) { %>
<a href="detail.html" class="layui-col-xs3 text">
<div><img src="<%= products[i].image %>"></div>
<p><%= products[i].name %></p>
<p class="price"><%= products[i].price %></p>
</a>
<% } %>
</table>
</div>
</div>
```