• vue 封装组件全过程


    Vue 封装组件的流程一般包括以下几个步骤:

    1. 创建组件文件:在项目中创建一个新的组件文件,一般以.vue为后缀,例如MyComponent.vue。

    2. 编写组件模板:在组件文件中编写组件的 HTML 结构,使用Vue的模板语法,例如:

    <template>
      <div>
        <h1>{{ title }}h1>
        <p>{{ content }}p>
      div>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 编写组件的样式:可以在组件文件中编写组件的样式,可以使用CSS、Sass、Less等预处理器,例如:
    <style scoped>
      .my-component {
        background-color: #f3f3f3;
        padding: 20px;
      }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 编写组件的逻辑:在组件文件中编写组件的逻辑,可以使用Vue的计算属性、方法等,例如:
    <script>
    export default {
      data() {
        return {
          title: 'Hello',
          content: 'This is my component'
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 导出组件:在组件文件的底部使用export default导出组件,例如:
    <script>
    export default {
      // ...
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 在其他组件中使用:在需要使用该组件的地方,引入该组件并在模板中使用,例如:
    <template>
      <div>
        <my-component>my-component>
      div>
    template>
    
    <script>
    import MyComponent from '@/components/MyComponent.vue'
    
    export default {
      components: {
        MyComponent
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    以上是封装一个简单的Vue组件的流程,完整的代码如下:

    
    <template>
      <div class="my-component">
        <h1>{{ title }}h1>
        <p>{{ content }}p>
      div>
    template>
    
    <script>
    export default {
      data() {
        return {
          title: 'Hello',
          content: 'This is my component'
        }
      }
    }
    script>
    
    <style scoped>
    .my-component {
      background-color: #f3f3f3;
      padding: 20px;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    
    <template>
      <div>
        <my-component>my-component>
      div>
    template>
    
    <script>
    import MyComponent from '@/components/MyComponent.vue'
    
    export default {
      components: {
        MyComponent
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    封装组件时,常用的事件有以下几种:

    1. 点击事件:可以使用@clickv-on:click绑定一个方法来处理点击事件,例如:
    <template>
      <button @click="handleClick">Click mebutton>
    template>
    
    <script>
    export default {
      methods: {
        handleClick() {
          // 处理点击事件的逻辑
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 输入事件:可以使用@inputv-on:input绑定一个方法来处理输入事件,例如:
    <template>
      <input type="text" @input="handleInput">
    template>
    
    <script>
    export default {
      methods: {
        handleInput(event) {
          const inputValue = event.target.value;
          // 处理输入事件的逻辑
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 自定义事件:可以使用$emit触发一个自定义事件,并在父组件中监听该事件,例如:
    
    <template>
      <button @click="handleClick">Click mebutton>
    template>
    
    <script>
    export default {
      methods: {
        handleClick() {
          this.$emit('customEvent', 'custom data');
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    
    <template>
      <div>
        <child-component @customEvent="handleCustomEvent">child-component>
      div>
    template>
    
    <script>
    import ChildComponent from '@/components/ChildComponent.vue'
    
    export default {
      components: {
        ChildComponent
      },
      methods: {
        handleCustomEvent(data) {
          // 处理自定义事件的逻辑
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在封装组件时,还需要注意以下几点:

    1. 组件的可复用性:尽量将组件设计成可复用的,避免与具体业务逻辑耦合过深。

    2. 组件的封装粒度:封装组件时需要考虑组件的封装粒度,尽量保持组件的功能单一,方便维护和复用。

    3. 组件的props和事件:通过props向组件传递数据,通过事件向父组件通信,遵循单向数据流的原则。

    4. 组件的样式隔离:使用scoped属性对组件的样式进行隔离,避免样式冲突。

    5. 组件的命名规范:遵循一定的命名规范,例如使用驼峰式命名或短横线命名。

    以上是封装组件时常用的事件和注意事项,希望对你有所帮助!

  • 相关阅读:
    java计算机毕业设计springboot+vue毕业设计选题系统
    1632. 矩阵转换后的秩 并查集+排序
    JavaScript FormData基本方法介绍
    HttpClient远程使用大全
    单商户商城系统功能拆解17—供应商分类
    C语言输出以下图案
    微信小程序——<image>图片组件图片显示闪烁
    有一个项目管理软件,名字叫8Mnaage PM!
    Thread类中run和start的区别
    js用普通DIV模拟A标签
  • 原文地址:https://blog.csdn.net/ACCPluzhiqi/article/details/134029691