ArticleDetail.vue
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<template>
<div id="articleDetail">
<div class="title">{{ detail.title }}</div>
<div class="time">{{ detail.createTime }}</div>
<div class="info_ctx_wrap tal" v-for="(html, index) in nodesHtml" :key="index">
<p v-html="html"></p>
<img v-if="index < nodesHtml.length - 1" class="img" :src="imgs[index]" />
</div>
</div>
</template>
<script>
export default {
name: 'Article',
data() {
return {
id: '',
detail: '',
content: '', //内容
imgs: [], //图片列表
nodesHtml: [], //切割的富文本元素
}
},
mounted() {
// console.log(this.$route.query)
this.id = this.$route.query.id
this.getConsultationDetail()
},
methods: {
// 获取底部栏目资讯
getConsultationDetail() {
if (!this.id) return
this.yxAxios.post(`${this.proxyUrl}/manage/info/content/detail/${this.id}`).then((res) => {
if (res.data.code == 200) {
let tempData = res.data.data
tempData.createTime = tempData.createTime.slice(0, 10)
tempData.content = tempData.content.replace(/\<img/gi, '<img class="rich-img" ') //正则给img标签增加class
let nodes = tempData.content
console.log('nodes:', nodes)
// 定义一个富文本中的图片数组
let imgs = [] //图片
// 定义一个富文本除了图片元素意外的元素
let nodesHtml = [] //切割的富文本元素
if (nodes && nodes.indexOf('src') >= 0) {
//当存在src图片才执行
nodes = nodes.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, (match, capture) => {
imgs.push(capture) //src的值
console.log(match)
console.log(capture)
console.log(imgs)
this.imgs = imgs
return '&**&' //把图片标签提取出来,并替换成&**&,以便后续来使用splic
})
// 把nodes中是把img标签替换成&**&的一个标签字符串
nodesHtml = nodes.split('&**&') //切割一下..拿到数组去遍历
// nodesHtml = nodesHtml.slice(0, -1) //最后是一个</p>标签,如果不去掉,最后会多一张图片...因为最后一个是p的闭合标签,去了也罢~
console.log('nodesHtml:', nodesHtml)
} else {
if (nodes) {
nodesHtml.push(nodes)
console.log('nodesHtml:', nodesHtml)
} else {
nodesHtml = ['暂无详情']
}
}
this.content = nodes
this.nodesHtml = nodesHtml
this.detail = tempData
document.title = this.detail.title
} else {
this.$toast.fail(res.message)
}
})
},
},
}
</script>
<style lang="scss" scoped>
#articleDetail {
width: 100%;
height: 100%;
overflow: auto;
box-sizing: border-box;
padding: 40px;
.title {
font-size: 36px;
text-align: left;
margin: 10px;
font-weight: bold;
}
.time {
font-size: 24px;
color: gray;
margin: 10px 0;
}
/* 基础信息 */
.info_wrap {
margin-top: 20px;
background-color: white;
}
.info_ctx_wrap {
.img {
width: 100%;
height: auto;
}
p {
font-size: 28px;
letter-spacing: 1px;
margin: 20px 0;
}
}
.tac {
text-align: center;
}
.tal {
text-align: left;
}
}
</style>
<style lang="scss">
// 长辈版
.elder {
#articleDetail .title,#articleDetail .time,#articleDetail .info_ctx_wrap p{
font-size: 36px;
}
}
</style>