vue组件data必须是函数

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <comp></comp>
      <comp></comp>
    </div>

    <template id="comp">
      <div id="">
        <p>{{message}}</p>
        <button @click="add">+</button>
        {{ count }}
        <button @click="sub">-</button>
      </div>
    </template>

    <script>
      const app = new Vue({
        el: '#app',
        data: {

        },
        components: {
          comp: {
            template: '#comp',
            data() {
              return {
                message: 'Hello Vue.js!',
                count: 1,
              }
            },
            methods: {
              add() {
                this.count++;
              },
              sub() {
                this.count--;
              }
            }
          },
        }
      })
    </script>
  </body>
</html>

vue组件data必须是函数

上一篇:vue子父访问parent root


下一篇:vue组件化