• 677 vue3组件化开发初体验:组件的名称,全局组件,局部组件


    认识组件化开发


    组件化开发


    Vue的组件化


    注册组件的方式


    注册全局组件


    全局组件的逻辑


    组件的名称


    注册局部组件


    布局组件注册代码


    02_注册全局组件.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
        <div id="app"></div>
    
        <!-- 子组件要挂载到这里,而不是挂载到<div id="app"></div> -->
        <template id="my-app">
          <component-a></component-a>
          <component-b></component-b>
    
          <!-- 推荐使用横线,不推荐使用驼峰 -->
          <!-- <ComponentName></ComponentName> -->
          <component-name></component-name>
        </template>
    
        <template id="component-a">
          <h2>{{title}}</h2>
          <p>{{desc}}</p>
          <button @click="btnClick">按钮点击</button>
        </template>
    
        <template id="component-b">
          <div>
            <input type="text" v-model="message"/>
            <h2>{{message}}</h2>
          </div>
        </template>
    
        <template id="component-c">
          <h2>ComponentC</h2>
        </template>
    
        <script src="../js/vue.js"></script>
        <script>
          const App = {
            template: "#my-app",
          };
    
          const app = Vue.createApp(App);
    
          // 使用app注册一个全局组件app.component()
          // 全局组件: 意味着注册的这个组件可以在任何的组件模板中使用
          app.component("component-a", {
            template: "#component-a",
            data() {
              return {
                title: "我是标题",
                desc: "我是内容, 哈哈哈哈哈",
              };
            },
            methods: {
              btnClick() {
                console.log("按钮的点击");
              },
            },
          });
    
          app.component("component-b", {
            template: "#component-b",
            data() {
              return {
                message: "Hello World",
              };
            },
          });
    
          app.component('ComponentName', {
            template: "#component-c"
          })
    
          app.mount("#app");
        </script>
      </body>
    </html>
    
    

    03_注册局部组件.html

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
      <div id="app"></div>
    
      <template id="my-app">
        <h2>{{message}}</h2>
        <component-a></component-a>
      </template>
    
      <template id="component-a">
        <h2>我是组件A</h2>
        <p>我是内容, 哈哈哈哈</p>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
    
        const ComponentA = {
          template: "#component-a"
        }
    
        const App = {
          template: '#my-app',
          components: {
            // key: 组件名称
            // value: 组件对象
            ComponentA: ComponentA
          },
          data() {
            return {
              message: "Hello World"
            }
          }
        }
    
        const app = Vue.createApp(App);
        // app.component("ComponentA", ComponentA);
        app.mount('#app');
      </script>
    </body>
    
    </html>
    

  • 相关阅读:
    列表数据类型内置方法
    WIN7局域网文件共享设置方法
    Python中if __name__ == '__main__':作用
    python练习2
    python练习1
    在centos6上面安装python3.4
    TCP连接之未连接队列的理解
    薛定谔把妹法
    js获取设备公网ip + 服务器根据公网ip 获取IP信息
    js获取设备内网ip
  • 原文地址:https://www.cnblogs.com/jianjie/p/14801403.html
Copyright © 2020-2023  润新知