如何在 nuxt-link 中添加 href?

问题描述 投票:0回答:5
vue.js vue-router nuxt.js
5个回答
2
投票

如果您在

<a>
内单击
<a>
(这正是
<nuxt-link>
生成的内容),您实际上是将单击事件发送到两个元素。这不是一个好的做法(甚至用 js 停止传播)。只是不要嵌套它

如果它必须位于“卡片”之上,也许可以使用 css 对其进行绝对定位。

尝试类似:

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

.card {
  position: relative;
}

.card__cta {
  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
}

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>

0
投票

这是@ArlanT 答案的扩展


它添加了

<slot/>
,以便您可以按如下方式在外部使用它。

<hyper-link class="anyClass" :url="myCustomUrl" @click.native="">Here lies my html</hyper-link>
<template>
  <component
    v-bind="linkProps(url)"
    :is="'hyperLink'"
  >
  <slot/>
  </component>
</template>

<script>
export default {
    props:['url'],
    name:'hyperLink',
    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>
</style>

0
投票
<a :href="card.url" target="_blank">{{ card.title }}</a>

0
投票
<NuxtLink to="/the-important-report.pdf" external>
Download Report

使用外部强制链接呈现为 a 标签而不是 Vue Router RouterLink。

© www.soinside.com 2019 - 2024. All rights reserved.