js 调用字符串类型的 js语句

data(){
  return{
    codeTxt:'(function() {console.log("run")})()'
 }
}
<textarea  v-model="codeTxt"></textarea>
  
<button class="btn" @click="runCode">运行</button>


runCode(){

// 第一种
let func = new Function(this.codeTxt); 
func();
  
// 第二种
eval(this.codeTxt)
  

// 第三种
setTimeout(this.codeTxt)

//第四种
<script>this.codeTxt</script>
  
}

  

posted @ 2020-07-21 15:01  花海无疆  阅读(244)  评论(0编辑  收藏  举报