3

currently I am scraping some website and return the value of the scraped data (from json file) into an HTML table in one of the component file in vue.js When displaying one of the value, I want this value to be put as href="link". However, since I am iterating all the data, "the link" is in the form of {{ row[8] }} which cannot be read by the Vue code. I tried:

<a v-bind:href="{{ row[8] }}"> View </a>
<a href={{ row[8] }}> View </a>
<a href="row[8]">View</a>

but none of these work. Here is my code:

        <tbody>
              <tr v-for="row in sesami">
                <td>{{ row[0] }}</td>
                <td>{{ row[1] }}</td>
                <td>{{ row[2] }}</td>
                <td>{{ row[3] }}</td>
                <td>{{ row[4] }}</td>
                <td>{{ row[5] }}</td>
                <td>{{ row[6] }}</td>
                <td>{{ row[7] }}</td>
                <td>
                  <a href="row[8]">View</a>
                </td>
              </tr>
        </tbody>

currently, with the code that I used, the hyperlink is mapped to the word "View" which is correct, but the value or the link is not inserted inside which caused the link when clicked to refresh the page instead. Please help....

Thank you

0

2 Answers 2

6

You do not need string interpolation when using the v-bind syntax as the scope of the expected argument is a javascript variable, e.g. row. Observe:

<a v-bind:href="row[8]"> View </a>

Which is syntactically the same as:

<a :href="row[8]"> View </a>
Sign up to request clarification or add additional context in comments.

5 Comments

yes thank you it works! May I know if I wanted the browser when click the "View" to be opened up a new tab should I just add a method .onclick?
@bryanbram No, you can just use regular HTML and add target="_blank" and the users browser settings will determine what to do (new tab, or new window)
thank you but adding target="_blank" didn't work in my case, is it that I need to setup something for my chrome?
@bryanbram No. You simply add it as a property: <a :href="row[8]" target="_blank"> View </a>
ahh yeah this one works. Cause initially I used <a v-bind:href="row[8]" target="_blank"> View </a> which didn't work. Thank you so much
1

You could iterate through the row items and you reach the index 8 bind the item to the href attribute :

<tbody>
       <tr v-for="row in sesami">
        <td v-for="(item ,i) in row">
            <template v-if="i===8">
                <a v-bind:href="item" target="_blank"> View </a>
             </template>
              <template v-else>
                {{item}}
             </template>
         </td>
        </tr>
</tbody>

2 Comments

thank you but the target = "_blank" didn't work in my case.
try out <a :href="item" target="_blank" rel="noopener noreferrer" >

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.