1

I have in Vue.js code

  <div class="footer-items">
  <span v-for="link in links" :key="link.name">
  <a :href="link.Link" class="tertiary--text footer-links">{{ link.name }}</a>
  </span>
</div>

The this gives me a footer with some links. I would like to have some of these links open an external window or tab.

The array that it loads from is:

   data: () => ({
   links: [
  { name: "Home", Link: "/dashboard" }, // needs to open same page
  { name: "PubMed", Link: "https://www.ncbi.nlm.nih.gov/pubmed" } // needs new window
  ]

This works but it open in the same window, I've tried:

        { name: "PubMed", Link: "https://www.ncbi.nlm.nih.gov/pubmed/,_blank" },  // error page

        { name: "PubMed", Link: "https://www.ncbi.nlm.nih.gov/pubmed/','_blank" }, // open same page

        { name: "PubMed", Link: "https://www.ncbi.nlm.nih.gov/pubmed/"','"_blank" },  // won't compile

        Link: "https://www.ncbi.nlm.nih.gov/pubmed/ target=_blank"  // opens same window

Is it possible programmatically send it to the href. I can't change the href because not all pages need to open in a new window.

I changed the code to:

          <a :href="link.Link" target="link.place" class="tertiary--text 
           footer-links">{{ link.name }}</a>

and added to the array a location:

          { name: "Home", Link: "/dashboard", place: "_self" },
           {name: "PubMed", Link: "https://www.ncbi.nlm.nih.gov/pubmed/",
           place: "_blank"
         },

And they all open in new windows. All help is appreciated.

3
  • you're missing the : in :target="link.place" Commented Jan 26, 2019 at 21:36
  • 1
    Like Daniel mentionned you forgot the :. Please consider adding rel="noopener noreferrer" for the links with target="_blank". You can learn more about this: mathiasbynens.github.io/rel-noopener Commented Jan 26, 2019 at 21:40
  • Thank you to all-- you are great Commented Jan 26, 2019 at 21:55

1 Answer 1

4

Try this (jsfiddle):

// Script
links: [
    { name: "Home", href: "/dashboard", target: "_self" },
    { name: "Google", href: "https://www.google.com/", target: "_blank" },
]

// Template
<a
    v-for="link in links"
    :key="link.name"
    :href="link.href"
    :target="link.target"
    rel="noopener noreferrer">
    {{ link.name }}
</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Works great-- Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.