后端返回一段字符串模板,前端如何解析?


 可以实现但是安全性会有问题,建议最好是不要这样来处理,可以考虑处理一下返回的

<template>
  <div id="app">
    <div v-html="template"></div>
  </div>
</template>

<script>
export default {
 name: "demo",
 data(){
    return{
    template :'',
    list:{
       name:'名称',
         arr:[
               { text:'1111', status:true },
               { text:'2222', status:false},
               { text:'3333', status:true },
               { text:'4444', status:false}
           ],
    },
temp2:"<div>${data.arr.map(item=>{return `<a href='${data.url}'>${item.name}</a>`})}</div>",
} }, mounted(){ this.template = this.test(this.list,this.temp)
 },
 methods:{
     test(data,temp){
       return new Function("data",`return \`${temp}\`;`)(data)
    }
 }
}
</script>    

  

模板用 compile 来实现。 https://cn.vuejs.org/v2/api/#Vue-compile

posted @ 2022-03-16 13:16  lilyliu329  阅读(388)  评论(0编辑  收藏  举报