简体   繁体   English

有没有办法在 Vue 中验证 HTML 语法?

[英]Is there a way to validate HTML syntax in Vue?

I have an component which basically takes everything from input and renders the html:我有一个组件,它基本上从输入中获取所有内容并呈现 html:

<template>
    <div>
        <input type="text" v-model="html">
        <VRuntimeTemplate :template="html" />
    </div>
</template>

<script>
import VRuntimeTemplate from 'v-runtime-template'

export default {
    data: () => ({
        html: ''
    }),
    components: {
        VRuntimeTemplate
    }
}
</script>

Now when I start to type then it will obviously throw me the error saying the input value doesn't have closing or ending tag.现在,当我开始输入时,它显然会向我抛出错误,指出输入值没有结束或结束标记。

在此处输入图片说明

Is there a way to validate HTML beforehand?有没有办法事先验证 HTML? It works fine with v-html but there's a problem with third party modules.它适用于v-html但第三方模块存在问题。

Sandbox: https://codesandbox.io/s/vruntimetemplate-27bdz?fontsize=14沙盒: https : //codesandbox.io/s/vruntimetemplate-27bdz? fontsize =14

Depending on when you need to validate the HTML there are a couple of options.根据您需要验证 HTML 的时间,有几个选项。

To validate input dynamically you can follow this answer: Check if HTML snippet is valid with JavaScript要动态验证输入,您可以按照以下答案进行操作: Check if HTML snippet is valid with JavaScript

If you want to validate the HTML output of the Vue Component there is a vue-cli plugin here: https://gitlab.com/html-validate/html-validate-vue如果你想验证 Vue 组件的 HTML 输出,这里有一个 vue-cli 插件: https : //gitlab.com/html-validate/html-validate-vue

I ran into the same issue and ended up going with the first option as I need to validate user supplied HTML that will be emitted in another vue app.我遇到了同样的问题,最终选择了第一个选项,因为我需要验证用户提供的 HTML,这些 HTML 将在另一个 vue 应用程序中发出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM