computed和methods问题

computed和methods问题

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./第三章源码/3-7/vue/vue.js"></script>
</head>
<body>
  <div id="root">
    <ul>
      <li v-for="user in shouldShowUsers" :key="user.id">{{ user.name }}</li>
    </ul>
  </div>
  <script>
    var vm = new Vue({
      el: '#root',
      data: {
        users: [
          {
            id: 1,
            name: 'a',
            show: true
          },
          {
            id: 2,
            name: 'b',
            show: true
          },
          {
            id: 3,
            name: 'c',
            show: false
          },
          {
            id: 4,
            name: 'd',
            show: true
          },
          {
            id: 5,
            name: 'e',
            show: false
          }
        ]
      },
      methods: {
        shouldShowUsers: function () {
          return this.users.filter(function (user) {
            return user.show
          })
        }
      },
    })
  </script>
</body>

</html>

当用computed时就能渲染出数据,用methods时就没有数据,为啥啊?

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,因为computed中存放的是属性,使用时直接通过属性名获取即可,但是methods中存放的是方法,使用时需要添加括号调用方法才可以。

示例:同学的代码,在使用shouldShowUsers方法时,添加一个括号即可

http://img1.sycdn.imooc.com//climg/5ec632380977e4dc11200182.jpg

如果我的回答帮助到了你,欢迎采纳,祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师