Vue2中@click在另一个组件中与v-html不起作用。

huangapple go评论77阅读模式
英文:

Vue2 @click doesn't work with v-html in another component

问题

I have a sign up form and there is an Alert component to show the errors, one of the conditionals is "You already have an account click here to log in".

but the error messages are strings that are passed as props for the component "Alert" and the actions "click here" is a link.

<!-- Parent.vue -->
<Alert v-if="!!errorField" :message="errorList[errorField].message" />

here it is the message

// Parent.vue
errorList = {
   hasAuth: {
      message: `You already have an account <a @click.prevent="hasAuthClick()">click here</a> to log in`
   }
}

the Alert component is simple it just receives the message as a prop and renders it with v-html

<!-- Child.vue -->
<span v-html="message"></span>

By investigations, I just realize that the link is not being compiled by vue, and that's why I can't use the @click on it, but I don't know how to make vue to compile.

英文:

I have a sign up form and there is an Alert component to show the errors, one of the conditionals is "You already have an account click here to log in".

but the error messages are strings that are passed as props for the component "Alert" and the actions "click here" is a link.

&lt;!-- Parent.vue --&gt;
&lt;Alert v-if=&quot;!!errorField&quot; :message=&quot;errorList[errorField].message&quot; /&gt;

here it is the message

// Parent.vue
errorList = {
   hasAuth: {
      message: `You already have an account &lt;a @click.prevent=&quot;hasAuthClick()&quot;&gt;click here&lt;/a&gt; to log in`
   }
}

the Alert component is simple it just recieve the message as prop and render with v-html

&lt;!-- Child.vue --&gt;
&lt;span v-html=&quot;message&quot;&gt;&lt;/span&gt;

By investigations, I just realize that the link is not being compiled by vue, and that's why I can't use the @click on it, but I don't know how to make vue to compile.

答案1

得分: 1

它将不适用于 v-html 指令,您应该使用Vue 插槽渲染函数

我更喜欢使用插槽来实现这个。

Alert.vue

<template>
   <div>
     <slot>默认插槽内容</slot>
     <slot name="message">消息插槽内容</slot>
   </div>
</template>

然后

<alert>
   默认插槽内容
   <template #message>
     您已经有一个帐户 <a @click.prevent="hasAuthClick()">点击这里</a> 登录
   </template>
</alert>

请注意,函数 hasAuthClick() 应该在 Alert.vue 外部的父组件/应用中定义。

查看我的另一个回答,有关渲染函数的内容:

使用函数在模板内部添加自定义HTML元素 Vue.js

英文:

It will not work with v-html directive and you should use Vue Slots or Render Functions

I would rather use Slots for this.

Alert.vue

&lt;template&gt;
   &lt;div&gt;
     &lt;slot&gt;default slot&lt;/slot&gt;
     &lt;slot name=&quot;message&quot;&gt;message slot&lt;/slot&gt;
   &lt;/div&gt;
&lt;/template&gt;

and then

&lt;alert&gt;
   default slot content
   &lt;template #message&gt;
     You already have an account &lt;a @click.prevent=&quot;hasAuthClick()&quot;&gt;click here&lt;/a&gt; to log in
   &lt;/template&gt;
&lt;/alert&gt;

Pay attention, the function hasAuthClick() should be defined outside Alert.vue, in the parent component/app.

Check my another antwort for the Render Functions

Adding custom HTML element inside template using functions Vue.js

huangapple
  • 本文由 发表于 2023年3月15日 21:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定