【问题标题】:Link to external url from json data by using Vue使用 Vue 从 json 数据链接到外部 url
【发布时间】:2020-03-13 00:12:10
【问题描述】:

我在链接到外部 url 时遇到了一个大问题。我从 json 获取这个 url 并连接到 HTML 标记。但是,当我单击图像时,我无法获取 url 数据并链接到该 url。

HTML

<section class="bg-light page-section" id="portfolio">
    <div class="container">
      <div class="row">
        <div class="col-lg-12 text-center"><br>
          <h2 class="section-heading text-uppercase works-text">Works</h2>
          <h3 class="section-subheading text-muted">Selected work that has been created with the help of many.</h3>
        </div>
      </div>
      <div class="row">
       <div class="col-md-4 col-sm-6 portfolio-item" v-for="(obj, key) in portfolioJSON" :key="key"  >
          <a :href="`${obj.url}`" class="portfolio-link" data-toggle="modal" target="_blank" >
            <div class="portfolio-hover">
              <div class="portfolio-hover-content">

              </div>
            </div>
            <img :src="`${obj.img}`" class="img-fluid" >
          </a>
        </div>

      </div>
    </div>
  </section>
export default {
    data() {
      return {
        portfolioJSON: [
           {
                img: require('../assets/img/sukimatch/sukimatch.png'),
                caption: 'Sukimatch',
                title: 'WEBSITE, BANNER DESIGN',
                url: "https://sukimatch-f887f.firebaseapp.com/"
            }     ]
    }
    }
}
```

【问题讨论】:

    标签: javascript json vue.js url hyperlink


    【解决方案1】:

    原则上,您的代码可以正常工作,但您所做的 img 标签没有意​​义。

    对于 SO 问题,最好使用一个小的、可重复的示例。我拿走了你的代码,让这个小提琴很有帮助。我放了一个:

    https://jsfiddle.net/79qLzv58/1/

    我使用的数据是:

        portfolio: [{
            img: 'https://dummyimage.com/300x200/000/fff&text=one',
            caption: 'Caption 1',
            title: 'Title 1',
            url: "https://destination-1.com/"
          },
          {
            img: 'https://dummyimage.com/300x200/000/fff&text=two',
            caption: 'Caption 2',
            title: 'Title 2',
            url: "https://destination-2.com"
          }
        ]
    

    在您的情况下,您不必使用类似的模板字符串

    <a :href="`${obj.url}`">
    

    但可以这样做:

    <a :href="obj.url">
    

    不过,如果您要进行任何形式的连接或修改 URL,那么模板字符串会很方便。

    我不确定 img 数据 URL 是否对您的应用至关重要,但 this answer 对此有更多了解。

    【讨论】:

    • 嗨@Andrew,非常感谢您的评论。这非常有帮助,我现在解决了这个问题!抱歉回复晚了,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多