如何在nuxt-link内添加href?

时间:2019-05-21 21:39:22

标签: vue.js vue-router nuxt.js nuxt

我有像这样的纸牌

+------+--------------------------------------------------------+
|groups|windows                                                 |
+------+--------------------------------------------------------+
|1     |[[2, 4, 2, 5], [30, 4, 2, 5], [30, 4, 2, 5]]            |
|2     |[[2, 1, 3, 7], [2, 4, 2, 9]]                            |
|3     |[[2, 4, 2, 5], [2, 4, 2, 5], [2, 1, 3, 7], [2, 1, 3, 7]]|
+------+--------------------------------------------------------+

点击带有nuxt-link的卡片应打开包含卡片详细信息的页面

单击href应打开外部网站

但是当我单击a-href的打开详细信息而忽略a-href

尝试将一些标签用于nuxt-link,但没有帮助

2 个答案:

答案 0 :(得分:0)

如果您在<a>内单击<a>(这正是<nuxt-link>生成的内容),则实际上是在将click事件发送到两个元素。这不是一个好习惯(甚至停止使用js进行传播)。只是不要嵌套

如果它必须在“卡片”的顶部,也许可以用css对其进行绝对定位。

尝试类似的东西:

<div class="card">
  <nuxt-link  :to="{ name: 'portfolio-slug', params: { slug: card.slug } }">
    {{ card.content }}
  </nuxt-link>
  <a class="goToHref" :href="card.link>Go to href</a>
</div>

.card {
  position: relative;
}

.goToHref {
  position: absolute;
  bottom: 24px; // depending where you need it, maybe you need top property
  right: 24px; // depending where you need it, maybe you need left property
}

答案 1 :(得分:0)

这似乎现在对我有用

在父组件中

 <template>
  <ul 
    v-if="loadedTertiaryMenu"  
    class="nav justify-content-end"
  >
    <li 
      v-for="item in loadedTertiaryMenu"
      :key="item.id"
      class="nav-item"
    >
      <NavLink
        :attributes="item"
      />
    </li>
  </ul>  
</template>

<script>
import NavLink from '@/components/Navigation/NavLink'

export default {
  name: 'TheNavigationTertiary',
  computed: {
    loadedTertiaryMenu() {
      return this.$store.getters.loadedTertiaryMenu
    }
  },
  components: {
    NavLink
  }
}
</script>

<style scoped lang="scss">

</style>

在子组件中

<template>
  <component
    v-bind="linkProps(attributes.path)"
    :is="NavLink"
    :title="attributes.title"
    :class="[ attributes.cssClasses ]"
    class="nav-link active"
    aria-current="page"
  >
  {{ attributes.label }}
  </component>
</template>

<script>
export default {
  name: 'NavLink',
  props: {
    attributes: {
      type: Object,
      required: true
    }
  },
  methods: {
    linkProps (path) {
      if (path.match(/^(http(s)?|ftp):\/\//) || path.target === '_blank') {
        return {
          is: 'a',
          href: path,
          target: '_blank',
          rel: 'noopener'
        }
      }
      return {
        is: 'nuxt-link',
        to: path
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>