[英]Vuejs How to have many <a> tags and hashtags within <router-link> or @click function
enter image description here在此处输入图像描述
i want to have a post card like twitter post card, if clicked on any part of the post card goes to post page, but when clicked on hashtags or links goes to the hashtag or link我想要一张像 twitter 明信片这样的明信片,如果点击明信片的任何部分会进入帖子页面,但是当点击主题标签或链接时会转到主题标签或链接
example below 1.下面的例子 1。
<div @click="gotoPostPage" class="text-weight-light text-justify">
{{ feed.content }}
<a href="https://google.com">check google</a> if you need more information then follow @benny(a tag) or follow these hashtags
#google #finda, #checks, now when i click within this post take me to post page
</div>
now this hits the gotoPostPage function even if link or a tag is clicked现在即使单击链接或标签,这也会触发 gotoPostPage 功能
using this also 2.也使用这个 2。
<router-link :to="`/post/${ feed.id }`">
{{ feed.content }}
<a href="https://google.com">check google</a> if you need more information then follow @benny(a tag) or follow
these hashtags
#google #finda, #checks, now when i click within this post take me to post page
</router-link>
goes to even when check google is clicked即使点击检查谷歌也会去
Please how can i resolve this, your help is highly appreciated, Thanks请问我该如何解决这个问题,非常感谢您的帮助,谢谢
Add @click.stop
to your links.将
@click.stop
添加到您的链接。 Eg:例如:
<a href="https://google.com" @click.stop>check google</a>
This will stop propagation of click
events to the parent DOM elements.这将停止将
click
事件传播到父 DOM 元素。
Note: @click.stop
is shorthand for @click="e => e.stopPropagation()"
.注意:
@click.stop
是@click="e => e.stopPropagation()"
的简写。
Update, based on data shown in comment:更新,基于评论中显示的数据:
I would avoid storing HTML id database.我会避免存储 HTML id 数据库。 That's a really bad pattern.
这是一个非常糟糕的模式。 You're supposed to detach the data from the UI layer.
您应该从 UI 层分离数据。 You have to allow the UI layer to change, based on device and medium.
您必须允许 UI 层根据设备和媒体进行更改。 I'd try to store that data as
string
s holding a hashtag name and, where the name is different than the hastag, as on object, containing both.我会尝试将该数据存储为包含主题标签名称的
string
s,并且在名称与主题标签不同的情况下,如在对象上,包含两者。
Something like this:像这样的东西:
const { createApp, reactive, computed, onMounted, toRefs } = Vue; createApp({ setup() { const state = reactive({ items: [], links: computed(() => state.items.map( item => ({ name: item.name || `#${item}`, url: item.url || `/hashtag/${item}` }) )) }); onMounted(async() => { /* replace this promise with an axios call to server which returns an array, similar to the one i'm passing here: (see axios call example below) */ const data = await new Promise(resolve => { setTimeout(() => { resolve([ 'one', 'two', 'three', 'печаль', 'грусть', { name: '#mens', url: '/hashtag/fıstıklıbaklava' }, 'чайная', 'джаз' ]); }); }) // example axios call: /* const data = await axios .get('http://endpoint-returning-data') .then(({data}) => data); */ state.items = data; console.log('See difference betwen items (received from server) \r\n\and links (same data, mapped for template):\r\n', {...state}, '\r\nMakes sense?'); }) return { ...toRefs(state), log: () => console.log('You clicked on app!') } } }).mount('#app')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script> <div id="app"> <div v-for="link in links" :key="link.url" @click="log"> <a :href="link.url" v-text="link.name" @click.stop></a> </div> </div>
As you can see, it produces the HTML from the data.如您所见,它从数据中生成 HTML。 The template also applies the
stopPropagation()
, in the v-for
.该模板还在
v-for
中应用了stopPropagation()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.