Instead of manipulating the DOM manually, you can use Vue's conditional rendering to achieve the same goal. Take a look at this solution below:
<template>
<div>
<template v-if="elementData">
<ul>
<li v-for="(paragraph, key) in elementData" :key="`paragraph-${key}`">
<span v-for="(part, partKey) in paragraph" :key="`part-${partKey}`">
<!-- Render an anchor tag if the part is a link -->
<template v-if="isLink(part)"
><a :href="part.link">{{ part.text }}</a></template
>
<!-- Else, just put output the part -->
<template v-else>{{ part }}</template>
</span>
</li>
</ul>
</template>
</div>
</template>
<script>
const about = [
[
"This is normal text ",
{
text: "im a link",
link: "https://www.stackoverflow.com",
},
", is not bad ",
],
["More text and text"],
];
export default {
...
data() {
return {
elementData: [],
};
},
mounted() {
this.setData();
},
methods: {
isLink(e) {
// You can change this condition if you like
return e && typeof e === "object" && e.link && e.text;
},
setData() {
this.elementData = about;
},
},
};
</script>
Notice how we just created elements based on conditions. We render an anchor tag if the part is an object, and has the link and text attribute. Else, we just display the string.
See live demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…