计算属性

2020-06-05
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>myvue-001</title>
  </head>
  <body>
    <div id="app">
        <div>当前时间{{getCurrentTime()}}</div>
        <!-- 计算过一次之后将不再计算 -->
        <div>计算属性{{getCurrentTimel}}</div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
    <script type="text/javascript">
      var misterzhang = new Vue({
        el:'#app',
        methods: {
          // 获取当前时间的方法
          getCurrentTime: function(){
            return Date.now();
          }
        },
        computed: {
          // 页面初始化时获取一次时间
          getCurrentTimel: function(){
            return Date.now();
          }
        }
      });
    </script>
  </body>
</html>