v-html rendering component problem

v-html rendering component problem

Since I have parsed HTML before, I want to use Vue drag and drop today to achieve automatic code generation similar to that of Kuai Station. As a result, I encountered the problem that the drag component could not be parsed in the past, because Vue's v-html can only parse HTML to prevent XSS attacks.

Ideas

First, implement a simple page divided into three parts: left, middle, and right. The left side is the component that needs to be dragged, the middle is used for component arrangement and display, and the right side is the parsed code

Component list code on the left

  <div ref="test" >
    <one-component :title="title[0]" element="<el-radio v-model='radio' label='1'>Alternative options</el-radio>"> 
      <el-radio slot="component" v-model="radio" label="1">Options</el-radio>
    </one-component>
  </div>
</template>

<script>
import OneComponent from '../components/oneComponent'
export default {
  name: '',
  data() {
    return {
        radio: '2',
        title: ['Radio single checkbox']
    }
  },
  components:{
    OneComponent
  },
 

}
</script>

<style lang="stylus" scoped>
</style>

Intermediate component display code

  <div class="all">
    <el-form label-width="80px" label-position="left" :model="ruleForm" :rules="rules">
      <el-form-item label="simulated width" prop="inputW">
        <el-input v-model="ruleForm.inputW" placeholder="Please enter width"></el-input>
      </el-form-item>
      <el-form-item label="Simulation height" prop="inputH">
        <el-input v-model="ruleForm.inputH" placeholder="Please enter height"></el-input>
      </el-form-item>
    </el-form>
    <Variablebox
      class="box"
      :width="ruleForm.inputW"
      :height="ruleForm.inputH"
    ></Variablebox>
  </div>
</template>
<script>
import Variablebox from "../components/Variablebox";
export default {
  name: "",
  data() {
    var checkSize = (rule, value, callback) => {
      console.log(value);
      if (value < 500 || value > 1000) {
        callback(new Error("A number between 500 and 1000 is recommended"));
      } else if (!Number.isInteger(Number(value))) {
        callback(new Error("Please enter a numeric value"));
      } else {
        callback();
      }
    };
    return {


      ruleForm: {
        inputW: 500,
        inputH: 500
      },

      rules:
        inputW: [{ validator: checkSize, trigger: "blur" }],
        inputH: [{ validator: checkSize, trigger: "blur" }]
      }
    };
  },
  methods: {
  },
  components:
    Variablebox
  }
};
</script>
<style lang="stylus" scoped>
.all
  padding: 0 20px
  display: flex
  flex-direction: column
</style>

Next, we implement the dragging of components using drag and drop to display the components on the Variablebox page. After using v-html failed, I searched on Baidu and found that it basically called using vue.Vue.compile( template ) and render, but there was no example.

compile compiles a template string into a render function, which means that render calls createElement in the end and converts it into HTML, but we render directly
</el-radio slot="component" v-model="radio" label="1"/>
So I personally feel that it doesn't work, and finally I can only try to create a new component and then mount it

   new Vue({
        // el: '#app'
        template: this.ele,
         data:{
           radio: '2'
        },
      }).$mount("#apps");

This will temporarily solve the problem.

Using v-html to render tags in vue

Get the background data with label content, which needs to be rendered to the page for display. The final effect is as follows: Graphics and text layout

1. First get the data and process it separately

2. Then output it in html

You may also be interested in:
  • Solution to the inability to modify the style of rich text rendered by vue through v-html instructions
  • Use v-html to solve the problem that html tags are not parsed in Vue.js rendering

<<:  How to completely uninstall iis7 web and ftp services in win7

>>:  Solution to 2059 error when connecting Navicat to MySQL

Recommend

Steps of an excellent registration process

For a website, it is the most basic function. So l...

Detailed steps for quick installation of openshift

The fastest way to experience the latest version ...

Implementation of MySQL Shell import_table data import

Table of contents 1. Introduction to import_table...

A detailed discussion on detail analysis in web design

In design work, I often hear designers participati...

How to use Linux paste command

01. Command Overview The paste command will merge...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

MySQL database deletes duplicate data and only retains one method instance

1. Problem introduction Assume a scenario where a...

Some tips on using the HTML title attribute correctly

If you want to hide content from users of phones, ...

SMS verification code login function based on antd pro (process analysis)

Table of contents summary Overall process front e...

Introduction and use of Javascript generator

What is a generator? A generator is some code tha...

CSS3 simple cutting carousel picture implementation code

Implementation ideas First, create a parent conta...

JavaScript Basics: Scope

Table of contents Scope Global Scope Function Sco...

Linux nohup command principle and example analysis

nohup Command When using Unix/Linux, we usually w...