简体   繁体   English

类型错误:无法在 vuejs 中读取未定义的属性(读取“添加”)

[英]TypeError: Cannot read properties of undefined (reading 'add') in vuejs

Error is happening because of this line "word.classList.add('tag')".由于这一行“word.classList.add('tag')”而发生错误。 When i was first adding a class here "if (word.includes('@')) {}" i was getting the same error, so i thought i have to push words into new array and loop them again, but the problem is something else.当我第一次在这里添加 class 时“if (word.includes('@')) {}”我遇到了同样的错误,所以我想我必须将单词推入新数组并再次循环它们,但问题是别的东西。

export default {
  data() {
    return {
      tweets: [],
      currentPage: 1,
      numberOfPages: 0,
      showPagination: 5,
      newPage: 0,
      text: [],
      arrayOfWords: [],
    }
  },
  methods: {
      getTweets(newCurrent) {
      this.tweets = []
      const API_URL = `${this.$server}/api/twitter/tweets`

      const params = {
        token: getUserToken(),
        page: this.$route.query.page,
        newCurrentPage: newCurrent,
      }
      axios.post(API_URL, null, { params }).then(res => {
        this.currentPage = res.data.page
        this.numberOfPages = res.data.numberOfPages

        res.data.tweets.forEach(tweet => {
          const tweetData = {
            id: tweet.id,
            tweet_text: tweet.tweet_text,
            twitter_name: tweet.twitter_name,
            twitter_username: tweet.twitter_username,
            added_at: moment(String(tweet.added_at)).format('MM/DD/YYYY hh:mm'),
          }
          this.tweets.push(tweetData)
          this.text.push(tweet.tweet_text)
        })

        this.tweets.forEach(tweet => {
          tweet.tweet_text.split(' ').forEach(word => {
            if (word.includes('@')) {
              this.arrayOfWords.push(word)
            }
          })
        })

        this.arrayOfWords.forEach(word => {
          word.classList.add('tag')
          console.log(word)
        })
      })
    },
  }

word is a string I think it has no classList , you can add classList to it if you like word是一个字符串我认为它没有classList ,如果你愿意,你可以向它添加classList

word.classList = word.classList || []

暂无
暂无

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

相关问题 类型错误:无法读取未定义的属性(读取“添加”) - TypeError: Cannot read properties of undefined (reading 'add') TypeError:无法读取未定义的属性(读取“集合”)-Vuejs 和 Firebase - TypeError: Cannot read properties of undefined (reading 'collection') - Vuejs and Firebase TypeError:无法使用 VueJS 读取未定义的属性(读取 '$refs') - TypeError: Cannot read properties of undefined (reading '$refs') with VueJS vuejs 2 组合 api: TypeError: Cannot read properties of undefined (reading 'length') - vuejs 2 composition api: TypeError: Cannot read properties of undefined (reading 'length') Jest Vuejs 测试 - 类型错误:无法读取未定义的属性(读取“参数”) - Jest Vuejs Test - TypeError: Cannot read properties of undefined (reading 'params') VueJS TypeError:无法读取未定义的属性(读取'toLowerCase') - VueJS TypeError: Cannot read properties of undefined (reading 'toLowerCase') 类型错误:无法读取未定义的属性(读取“$router”)vuejs - TypeError: Cannot read properties of undefined (reading '$router') vuejs “TypeError”:无法读取 Vuejs 中未定义的属性? - "TypeError": Cannot read properties of undefined in Vuejs? 无法读取未定义的属性(读取“$set”)vuejs - Cannot read properties of undefined (reading '$set') vuejs TypeError:无法读取未定义的属性(读取“add_cookies”) - TypeError: Cannot read properties of undefined (reading 'add_cookies')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM