vuejs如何拥有许多< a> <路由器ltink> gt; gt;或@Click函数

发布于 2025-02-08 07:15:04 字数 1094 浏览 2 评论 0原文

在此处输入映像

我想拥有一张明信片,例如Twitter Post Card,如果单击任何内容明信片的一部分转到发布页面,但是当单击标签或链接时,将转到

下面的标签或链接示例 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>

点击了链接

现在,即使使用此链接或标签也 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>

即使检查google 请点击

请如何解决这个问题,您的帮助非常感谢,非常感谢您的帮助, 谢谢

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

example below
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

using this also
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-02-15 07:15:04

@单击添加到您的链接。例如:

<a href="https://google.com" @click.stop>check google</a>

这将停止的传播>单击事件到父元素元素。


注意:@click.stop@conce@click =“ e =&gt; e.stoppropagation()”

在这里


更新,根据注释中显示的数据:

我将避免存储HTML ID数据库。那是一个非常糟糕的模式。您应该从UI层分离数据。您必须允许根据设备和介质更改UI层。我会尝试将该数据存储为String s保存标签名称,而该名称与Hastag不同,如在对象上,都包含两个。

这样的事情:

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>

如您所见,它从数据中产生HTML。该模板还应用stopPropagation(),在v-for中。

Add @click.stop to your links. E.g:

<a href="https://google.com" @click.stop>check google</a>

This will stop propagation of click events to the parent DOM elements.


Note: @click.stop is shorthand for @click="e => e.stopPropagation()".

Docs here.


Update, based on data shown in comment:

I would avoid storing HTML id database. That's a really bad pattern. You're supposed to detach the data from the UI layer. You have to allow the UI layer to change, based on device and medium. I'd try to store that data as strings holding a hashtag name and, where the name is different than the hastag, as on object, containing both.

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. The template also applies the stopPropagation(), in the v-for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文