我们注册一个 button-counter 组件,在每次点击后,计数器会加 1:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Vue 测试实例</title>
    <script src="https://unpkg.com/vue@next"></script>
  </head>
  <body>
    <div id="app">
      <button-counter></button-counter>
    </div>

    <script>
      // 创建一个Vue 应用
      const app = Vue.createApp({})

      // 定义一个名为 button-counter 的新全局组件
      app.component('button-counter', {
        data() {
          return {
            count: 0,
          }
        },
        template: `
        <button @click="count++">
          点了 {{ count }} 次!
        </button>`,
      })
      app.mount('#app')
    </script>
  </body>
</html>

image
image

posted @ 2022-04-28 09:08  wjxuriel  阅读(95)  评论(0)    收藏  举报