简体   繁体   English

将 html 元素字符串传递给 vue 自定义指令,eslint 问题

[英]Pass html element string to vue custom directive, issue with eslint

I created two custom vue directives to insert an html element after or before another element, I pass an html element string to the element I apply the directive but VS Code is giving me the following error:我创建了两个自定义 vue 指令来在另一个元素之后或之前插入一个 html 元素,我将一个 html 元素字符串传递给应用该指令的元素,但 VS Code 给了我以下错误:

Parsing error: unexpected-character-in-attribute-name.eslint-plugin-vue

This is how I'm doing it:这就是我的做法:

<rad-stack title="Precio final" v-insert-before="'<div class="RADcard3_texts_info_divider"></div>'">{{ item.finalPrice }} €</rad-stack>

My directive looks like this:我的指令如下所示:

Vue.directive('insert-before', {
    isLiteral: true,
    inserted: (el, binding, vnode) => {
        el.parentNode.insertBefore(binding.value, el);
    }
});

The issue is with the Double Quotes "" .问题在于双引号"" Since the html attribute values are wrapped with Double quotes we can't use them inside the string.由于 html 属性值用双引号括起来,我们不能在字符串中使用它们。

You can assign the value to the instance variable and refer it in the template as follows,您可以将值分配给实例变量并在模板中引用它,如下所示,

Vue Template Vue 模板

<rad-stack title="Precio final" v-insert-before="prefixMsg">{{ item.finalPrice }} €</rad-stack>

Vue Script Vue 脚本

data() {
 return {
   prefixMsg: '<div class="RADcard3_texts_info_divider"></div>'
 }
}

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

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