let data={
title:'标题',
content:'内容',
list:[
{ text:'1111', status:true },
{ text:'2222', status:false},
{ text:'3333', status:true },
{ text:'4444', status:false}
]
}
let temp=`<div>
<h3 style="font-size:20px; color:red">${data.title}</h3>
<div style="font-size:15px; color:blue">${data.content}</div>
<ul>
${data.list.map(item=>{
if(item.status){
return `<li style="color:red">${item.text}</li>`
}else{
return `<li style="color:black">${item.text}</li>`
}
}).join('')}
</ul>
</div>`
console.log(temp)
打印html代码
<div>
<h3 style="font-size:20px; color:red">标题</h3>
<div style="font-size:15px; color:blue">内容</div>
<ul>
<li style="color:red">1111</li><li style="color:black">2222</li><li style="color:red">3333</li><li style="color:black">4444</li>
</ul>
</div>
现在temp通过接口返回获取,这个时候该如何编译才能正常显示
你可以看下demo,可以实现但是安全性会有问题,建议最好是不要这样来处理,可以考虑处理一下返回的模板用
compile
来实现。 API — Vue.js