Disabling wrapping link in Svelte

Viewed 399

I am trying to make a component work as link based on a variable. Is it possible to do that?

So sometimes I want it to work like this:

<a href="...">
   <Component />
</a>

And sometimes like this:

<Component />

I need it to work as a link as I need to use target='_top' on it. window.top.location.href=... does not work in my case.

1 Answers

Have you tried window.open(href, '_top')?

Otherwise, what you asked can only be achieved with a {#if ...}:

{#if target}
  <a {href} {target}>
    <Component />
  </a>
{:else}
  <Component />
{/if}

There are other ways: preventing default click event, preventing click events by CSS, probably more... But I think the first suggestion with window.open should workd for you.

Related