mrr.sj.front/pages/selfOperated/components/imgAndText.vue

144 lines
3.9 KiB
Vue
Raw Normal View History

2026-03-24 11:45:13 +08:00
<template>
<view class="container">
<!-- uView组件适配确保uv-divider在nvue中正常渲染需确保uView已配置nvue支持 -->
<uv-divider text="图文详情" lineColor="#E0E1E1" textColor="#3D3D3D" textSize="24rpx"></uv-divider>
<!-- 列表渲染修正为数组遍历原代码初始为对象会导致v-for报错 -->
<!-- <view v-for="(item, index) in detailList" :key="index" class="detail-item">
<image
:src="item.value"
v-if="item.type === 3"
class="detail-img"
mode="widthFix"
/>
<view v-if="item.type === 2" class="detail-text-container">
<text class="detail-text">{{ item.value }}</text>
</view>
<uv-divider dashed v-if="item.type === 1" ></uv-divider>
<rich-text
:nodes="formatRichText(item.value)"
v-if="item.type === 1"
class="detail-rich-text"
/>
</view> -->
<view class="detail-item">
<!-- <rich-text
:nodes="info"
class="detail-rich-text"
/> -->
<uv-parse :content="info"></uv-parse>
</view>
</view>
</template>
<script>
export default {
props: {
info: {
type: String,
default: "[]" // 默认值设为空数组JSON避免parse报错
}
},
data() {
return {
detailList: [] // 修正初始类型为数组原代码是对象v-for无法遍历
};
},
mounted() {
//this.parseInfoData();
},
methods: {
// 解析传入的JSON字符串确保生成数组
parseInfoData() {
try {
// 防止info为空或格式错误导致崩溃
const parsedData = JSON.parse(this.info);
// 确保最终是数组(若后端返回对象,转为数组)
this.detailList = Array.isArray(parsedData) ? parsedData : [];
} catch (error) {
console.error("解析图文详情数据失败:", error);
this.detailList = [];
}
},
// 处理富文本nodesnvue中rich-text样式需内嵌外部样式难穿透
formatRichText(nodes) {
console.log(111111,nodes);
// 若nodes是字符串如HTML片段转为标准rich-text nodes格式
if (typeof nodes === "string") {
return [{
name: "div",
attrs: {
style: "font-size:26rpx;color:#3D3D3D;line-height:40rpx;margin:10rpx 0;"
},
children: [{
type: "text",
text: nodes
}]
}];
}
// 若已是nodes格式补充基础样式避免文字样式丢失
return JSON.parse(JSON.stringify(nodes)).map(node => {
if (node.attrs && !node.attrs.style) {
node.attrs.style = "font-size:26rpx;color:#3D3D3D;line-height:40rpx;";
}
return node;
});
}
}
};
</script>
<style>
/* 1. 移除scoped和lang="less"nvue不支持样式全部平级写 */
/* 容器基础样式:仅保留布局和背景,无字体样式 */
.container {
background-color: #fff;
overflow: hidden;
margin: 20rpx 24rpx;
padding: 20rpx;
border-radius: 16rpx;
/* nvue对box-shadow支持有限建议移除避免渲染异常 */
}
/* uView divider适配调整间距若组件默认样式不符合手动覆盖 */
.uv-divider {
margin: 0 0 20rpx 0 !important; /* 强制调整分隔线与内容的间距 */
}
/* 列表项:垂直间距控制 */
.detail-item {
margin-bottom: 20rpx;
width: 658rpx;
}
.detail-item:last-child {
margin-bottom: 0;
}
/* 图片样式widthFix确保宽度自适应高度按比例 */
.detail-img {
width: 658rpx;
border-radius: 16rpx;
margin: 10rpx 0; /* 与上下内容的间距 */
}
/* 纯文本容器仅控制布局字体样式在text标签上 */
.detail-text-container {
margin: 10rpx 0;
}
/* 纯文本样式直接写在text标签class上nvue不继承 */
.detail-text {
font-weight: 400;
font-size: 26rpx;
color: #3D3D3D;
line-height: 40rpx;
text-align: left;
}
/* 富文本容器控制整体间距内部样式靠formatRichText内嵌 */
.detail-rich-text {
margin: 10rpx 0;
}
</style>