帮忙解释一下 this.currentCategory = id ? this.categories.find (category => category.id === id) : {}

我们利用 this.categories.find(category => category.id === id) 根据当前传入的 id 从所有分类中找到选中的分类,并赋值给 currentCategory 属性

  1. this.categories.find(category => category.id === id)这是个有返回值的表达式?第一次见到这个表达式,求解释
  2. data = {
    currentCategory:{}   // 这个为什么要赋值一个对象呢?为什么不直接给一个 整形 0 呢?
    }
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
liyu001989
最佳答案
  1. https://developer.mozilla.org/en-US/docs/W...

    file

  2. 对象可以存储更多的信息,比如名称,id 等,默认是个空对象
6年前 评论
讨论数量: 2
liyu001989
  1. https://developer.mozilla.org/en-US/docs/W...

    file

  2. 对象可以存储更多的信息,比如名称,id 等,默认是个空对象
6年前 评论

@liyu001989 在教程中为什么要给一个默认值0呢?为什么不直接赋值呢
this.currentCategory = this.categories.find(category => category.id === id)
从别的分类切换到 话题分类的时候需要赋值 0 然后数据库找不到分类为0的就返回全部话题了 是么?

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!