简体   繁体   English

使用 gatsby-source-google-spreadsheet 将 Sheets 中的链接插入 Gatsby

[英]Inserting links from Sheets into Gatsby with gatsby-source-google-spreadsheet

I'm using the gatsby-source-google-spreadsheet plugin in Gatsby to create a list of elements on a page.我在 Gatsby 中使用 gatsby-source-google-spreadsheet 插件在页面上创建元素列表。 The plugin is set up with my Sheets and is working well.该插件与我的表格一起设置并且运行良好。 I am struggling with how to insert a link as an <a href> when I'm pulling it in from a Sheet.当我从工作表中拉入链接时,我正在努力解决如何将链接作为<a href>插入。

src/pages/index.js : src/pages/index.js

import React from "react"
import { graphql } from "gatsby"



export default ({ data }) => {
  const sheetData = data.allGoogleSpreadsheetSheetSheet.nodes;
  const rendered = sheetData.map(item =>
    <div className="section">

      <div className="section__when"> {item.day}, {item.date} at {item.time} </div>
      <div className="section__host"> {item.event}, {item.host} </div>
      <div className="section__link"> <a href="{item.link}" target="_blank">Access here. </a></div>
      <div className="section__description"> {item.description} </div>
    </div>);

  return rendered;
};


export const query = graphql`
  query {
    allGoogleSpreadsheetSheetSheet {
      nodes {
        event
        host
        link
        day
        time
        date
        description
      }
    }
  }
`;

This gives me an appropriate list but when I click on the link it tries to take me to: http://localhost:8000/%7Bitem.link%7D , which is obviously wrong.这给了我一个合适的列表,但是当我点击链接时,它试图把我带到: http://localhost:8000/%7Bitem.link%7D ,这显然是错误的。 Right now, my Sheets just has the link https://www.google.com for each entry.现在,我的表格只有每个条目的链接https://www.google.com How do I change the way the link div is structured to make it take me to an external link?如何更改链接 div 的结构以使其转到外部链接?

If you check your link you'll see that there is your variable name inside %7Bitem.link%7D means literally {item.link} , so your code doesn't recognize the variable and it's inserting as string {item.link} .如果您检查您的链接,您会看到%7Bitem.link%7D中有您的变量名称,字面意思是{item.link} ,因此您的代码无法识别该变量,而是作为字符串{item.link}插入。

Assuming that you are retrieving and receiving the variables the data properly, as you said.假设您正在正确检索和接收变量数据,如您所说。

Try this:尝试这个:

import React from "react"
import { graphql } from "gatsby"



export default ({ data }) => {
  const sheetData = data.allGoogleSpreadsheetSheetSheet.nodes;
  const rendered = sheetData.map(item =>
    <div className="section">

      <div className="section__when"> {item.day}, {item.date} at {item.time} </div>
      <div className="section__host"> {item.event}, {item.host} </div>
      <div className="section__link"> <a href=`${item.link}` target="_blank">Access here. </a></div>
      <div className="section__description"> {item.description} </div>
    </div>);

  return rendered;
};


export const query = graphql`
  query {
    allGoogleSpreadsheetSheetSheet {
      nodes {
        event
        host
        link
        day
        time
        date
        description
      }
    }
  }
`;

Spot the difference in your anchor tag: <a href=`${item.link}` target="_blank"> <a href=`${item.link}` target="_blank">锚标签的不同之处: <a href=`${item.link}` target="_blank">

Basically you are using template literals from ES6.基本上,您使用的是 ES6 中的模板文字 They are string literals allowing embedded expressions and are enclosed by the backtick (`) (grave accent) character instead of double or single quotes.它们是允许嵌入表达式的字符串文字,并用反引号 (`)(重音符号)字符而不是双引号或单引号括起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM