Vue局部组件

局部组件

📌全局注册往往是不够理想的。比如,如果你使用一个像 webpack 这样的构建系统,全局注册所有的组件意味着即便你已经不再使用一个组件了,它仍然会被包含在你最终的构建结果中。这造成了用户下载的 JavaScript 的无谓的增加。

在这些情况下,你可以通过一个普通的 JavaScript 对象来定义组件:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Local Component Example</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
  <div id="app"></div>

  <script>
    // 定义一个简单的局部组件
    const LocalComponent = {
      template: '<div class="local-component">{{ message }}</div>',
      data() {
        return {
          message: 'Hello from LocalComponent!'
        };
      }
    };

    // 创建 Vue 实例,并在组件选项中注册局部组件
    new Vue({
      el: '#app',
      components: {
        'local-component': LocalComponent
      },
      template: `
        <div>
          <h1>Hello from Parent Component!</h1>
          <local-component></local-component>
        </div>
      `
    });
  </script>
</body>
</html>


只在某个组件内部使用的组件,可以直接在 components 选项中注册。例如:

new Vue({
  el: '#app',
  components: {
    'component-a': ComponentA,
    'component-b': ComponentB
  }
})

📌谁注册谁使用(从属关系)

对于 components 对象中的每个 property 来说,其 property 名就是自定义元素的名字,其 property 值就是这个组件的选项对象。

注意局部注册的组件在其子组件中_不可用_。例如,如果你希望 ComponentAComponentB 中可用,则你需要这样写:

var ComponentA = { /* ... */ }

var ComponentB = {
  components: {
    'component-a': ComponentA
  },
  // ...
}

或者如果你通过 Babel 和 webpack 使用 ES2015 模块,那么代码看起来更像:

import ComponentA from './ComponentA.vue'

export default {
  components: {
    ComponentA
  },
  // ...
}

注意在 ES2015+ 中,在对象中放一个类似 ComponentA 的变量名其实是 ComponentA: ComponentA 的缩写,即这个变量名同时是:

  • 用在模板中的自定义元素的名称
  • 包含了这个组件选项的变量名

步骤

  1. 定义组件:定义一个包含组件配置的对象。创建组件结构 template,创建组件实例 const
  2. 在父组件中注册组件:在父组件的 components 选项中注册该组件。
  3. 在模板中使用组件:在父组件的模板中使用定义好的局部组件。
<body>
    <div id="app">
        {{msg}}
        <!-- 4.使用组件 -->
        <my-com></my-com>
    </div>
</body>
<script src="libs/vue.js"></script>

<!-- 1.创建组件模板 -->
<template id="temp">
    <div>
        <h1>{{msg}}</h1>
    </div>
</template>

<script>
    // 2.定义组件
    const com = {
        template: "#temp",
        data() {
            return {
                msg: "helle temp 3"
            }
        }
    }
    
  new Vue({
        el: "#app",
        data: {
            msg: "hello vue"
        },
        components: {
            // 3.注册局部组件,谁注册谁使用!
            "my-com": com,
        }
    })
</script>

动态

<!DOCTYPE html>
<html>
<head>
  <title>Vue Dynamic Local Component Example</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
  <div id="app"></div>

  <script>
    // 定义两个局部组件
    const ComponentA = {
      template: '<div class="component-a">Hello from Component A</div>'
    };

    const ComponentB = {
      template: '<div class="component-b">Hello from Component B</div>'
    };

    // 创建 Vue 实例
    new Vue({
      el: '#app',
      data: {
        currentComponent: 'component-a'
      },
      components: {
        'component-a': ComponentA,
        'component-b': ComponentB
      },
      template: `
        <div>
          <h1>Dynamic Local Component</h1>
          <button @click="switchComponent">Switch Component</button>
          <component :is="currentComponent"></component>
        </div>
      `,
      methods: {
        switchComponent() {
          this.currentComponent = this.currentComponent === 'component-a' ? 'component-b' : 'component-a';
        }
      }
    });
  </script>
</body>
</html>

场景

  1. 局部功能模块:适用于在特定页面或功能模块中使用的组件。
  2. 父子组件通信:当组件只在父组件或某个特定的组件树中使用时,可以作为局部组件注册。
  3. 避免全局污染:防止全局注册的组件过多,导致命名冲突和管理困难。

优点

  1. 避免全局命名冲突:局部组件只在父组件的上下文中可用,不会污染全局命名空间。
  2. 提高代码可读性:将组件限制在使用它们的上下文中,代码更易读和理解。
  3. 易于调试:局部组件的作用范围明确,更容易定位和修复问题。
  4. 模块化开发:支持将不同功能模块化,提高代码的复用性和维护性

相关推荐

  1. Vue局部组件

    2024-06-18 12:54:05       35 阅读
  2. Vue2组件注册:全局组件局部组件

    2024-06-18 12:54:05       51 阅读
  3. 如何使用vue定义组件之——全局or局部

    2024-06-18 12:54:05       41 阅读
  4. Vue中实现【组件局部刷新】及【页面刷新】

    2024-06-18 12:54:05       37 阅读
  5. Vue.component v2v3注册(局部与全局)组件使用详解

    2024-06-18 12:54:05       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-18 12:54:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-18 12:54:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-18 12:54:05       82 阅读
  4. Python语言-面向对象

    2024-06-18 12:54:05       91 阅读

热门阅读

  1. 深入理解HTTP协议——针对TCP的优化建议

    2024-06-18 12:54:05       36 阅读
  2. Doris 系统日志和审计日志

    2024-06-18 12:54:05       27 阅读
  3. 【人工智能】深度解读 ChatGPT基本原理

    2024-06-18 12:54:05       36 阅读
  4. mysql竖表变横表不含聚合

    2024-06-18 12:54:05       33 阅读
  5. Chrome 报错: ERR_ACCESS_DENIED

    2024-06-18 12:54:05       39 阅读
  6. AI大战:通用VS垂直模型,谁主未来?

    2024-06-18 12:54:05       29 阅读
  7. xpath爬取4399的最新游戏系列

    2024-06-18 12:54:05       27 阅读
  8. Day05 数组

    2024-06-18 12:54:05       31 阅读
  9. C++中的八大设计原则

    2024-06-18 12:54:05       25 阅读
  10. window环境C++读取带中文的文档乱码问题

    2024-06-18 12:54:05       34 阅读
  11. 使用微信小程序制作画布

    2024-06-18 12:54:05       28 阅读