首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue.js -将el元素注入html

Vue.js -将el元素注入html
EN

Stack Overflow用户
提问于 2020-06-27 23:08:33
回答 1查看 56关注 0票数 0

我有在线考试的网站。我在测试中创建了一个题,题目是“填空”,意思是填空格里的单词。

问题来自服务器,它是一个类似于该"Today is a [1] day, and i should [2] today"的字符串。我想要做的是获得这个字符串,并用el-input替换所有的[]

我也做过类似的事情

代码语言:javascript
复制
<template>
 <div class="d-flex flex-column mg-t-20 pd-10">
 <h6 class="tx-gray-800">Fill in the blank areas the missing words</h6>
 <div class="mg-t-20" v-html="generateFillBlankQuestion(question.question)" />
 </div>
</template>

<script>
  export default {
   name: 'FillBlank',
   directives: {},
   props: [ 'question' ],
   components: {
   },
   computed: {},
   data() {
     return {
       input: ''
     }
   },
   filters: {},
   created() { 
   },
   methods: {
       generateFillBlankQuestion(question) {
        var matches = question.match((/\[\d\]/g))
        console.log(matches)
        matches.forEach((element) => {
          console.log(element)
         
         question = question.replace(element, '<el-input />')
       })
       console.log(question)
       return question
      } 
   }
 }

在这行输入中,我将[]替换为question = question.replace(element, '<el-input />')。由于某些原因,当我尝试将其替换为<el-input>时,它不会呈现它。但是如果我使用<input type='text'>,它会渲染它。是否可以注入el elements

EN

回答 1

Stack Overflow用户

发布于 2020-06-28 01:06:41

如果您没有使用Vue运行时模板编译器,则不能在v-html中呈现Vue组件。你应该这样做:

代码语言:javascript
复制
<template>
 <div class="d-flex flex-column mg-t-20 pd-10">
   <h6 class="tx-gray-800">Fill in the blank areas the missing words</h6>
   <div class="mg-t-20">
     <template v-for="(word,idx) in wordList">
       <el-input v-if="word.blank" v-model="word.value" :key="idx" />
       <template v-else>{{ word.text }}</template>
     </template>
   </div>
 </div>
</template>

<script>
export default 
{
   name: 'FillBlank',
   props: 
   { 
     question:
     {
       type: String,
       default: ''
     }
   },
   computed: 
   {
     wordList()
     {
       const words = this.question.split(' ');
       return words.map(word =>
       ({
         value: '',
         text: word,
         blank: /^\[\d+\]$/.test(word),
       }));
     }
   }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62611715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档