简体   繁体   English

为什么这个组件创建者返回未定义?

[英]Why is this component creator returning undefined?

import axios from "axios"

 const Card = (article) => {
  let cardWrap = document.createElement('div')
  let headDiv = document.createElement('div')
  let authorDiv = document.createElement('div')
  let picWrap = document.createElement('div')
  let imgAuthor = document.createElement('img')
  let authorSpan = document.createElement('span')

  cardWrap.classList.add('card')
  headDiv.classList.add('headline')
  authorDiv.classList.add('author')
  picWrap.classList.add('img-container')

  cardWrap.appendChild(headDiv)
  cardWrap.appendChild(authorDiv)
  authorDiv.appendChild(picWrap)
  picWrap.appendChild(imgAuthor)
  authorDiv.appendChild(authorSpan)

  headDiv.textContent = (article.headline)
  imgAuthor.src = (article.authorPhoto)
  authorSpan.textContent = (article.authorName)

  cardWrap.addEventListener(('click'), e => {
    console.log(article.headline)

  return cardWrap
  })


const cardAppender = (selector) => {
  axios.get('https://lambda-times-api.herokuapp.com/articles')
      .then((articleData) => {
        console.log(articleData.data.articles.javascript)
        articleData.data.articles.javascript.forEach(articleNew => {
          console.log(articleNew)
          let newCard = Card(articleNew);
          console.log(newCard)
        });
      })
      .catch((err) => {
        console.log(err)
      })

this is the console log for articleNew这是 articleNew 的控制台日志

{id: "1e4d8724-5973-4b5b-84d9-a30a3c5adb70", headline: "ES8: The Next Step in the Evolution of Javascript and What it Means For Your Projects", authorPhoto: "https://tk-assets.lambdaschool.com/08d1372e-e393-47f1-ac44-fcb7d0baf0e2_sir.jpg", authorName: "SIR RUFF'N'STUFF"} {id:“1e4d8724-5973-4b5b-84d9-a30a3c5adb70”,标题:“ES8:Javascript 演变的下一步及其对您的项目意味着什么”,作者照片:“https://tk-assets.lambdaschool. com/08d1372e-e393-47f1-ac44-fcb7d0baf0e2_sir.jpg”,作者姓名:“SIR RUFF'N'STUFF”}

{id: "553e2401-c95d-4029-89b8-fc6bdb60ae5e", headline: "Type Coercion: Why Does NaN?== NaN, and Other Strange Occurrences": authorPhoto: "https.//tk-assets.lambdaschool.com/a9471235-ed71-4b11-ae15-5a4fa1151d30_bones,jpg": authorName. {id:“553e2401-c95d-4029-89b8-fc6bdb60ae5e”,标题:“类型强制:为什么 NaN?== NaN 和其他奇怪的事件”:作者照片:“https.//tk-assets.lambdaschool.com/ a9471235-ed71-4b11-ae15-5a4fa1151d30_bones,jpg": 作者姓名。 "BONES R. LIFE"} “骨头河生活”}

{id: "9c2ed89e-7150-4cd6-a5c2-4a4f3d6932c8", headline: "When to Rest, When to Spread: Why There Are Two Meanings Behind '...'", authorPhoto: "https://tk-assets.lambdaschool.com/44260ce3-c8f0-4db8-bc1d-9877662fdf96_puppers.jpg", authorName: "PUPPER S. DOGGO"} {id:“9c2ed89e-7150-4cd6-a5c2-4a4f3d6932c8”,标题:“何时到 Rest,何时传播:为什么 '...' 背后有两种含义”,作者照片:“https://tk-assets. lambdaschool.com/44260ce3-c8f0-4db8-bc1d-9877662fdf96_puppers.jpg”,作者姓名:“PUPPER S. DOGGO”}

{id: "b3af02ec-5733-4a14-8876-5db708d20051", headline: "Typescript: Ten Things you Should Know Before Building Your Next Angular Application", authorPhoto: "https://tk-assets.lambdaschool.com/08d1372e-e393-47f1-ac44-fcb7d0baf0e2_sir.jpg", authorName: "SIR RUFF'N'STUFF"} {id:“b3af02ec-5733-4a14-8876-5db708d20051”,标题:“Typescript:在构建下一个 Angular 应用程序之前应该知道的十件事”,作者照片:“https://tk-assets.lambdaschool.com/08d1372e- e393-47f1-ac44-fcb7d0baf0e2_sir.jpg”,作者姓名:“SIR RUFF'N'STUFF”}

I dont understand why newCard is logging undefined?我不明白为什么 newCard 记录未定义?

cardWrap.addEventListener(('click'), e => {
    console.log(article.headline)

  return cardWrap // <== this should be outside the event listener
  })

Your return is inside addEventListener.您的回报在 addEventListener 内。

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

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