当前位置: 首页 > news >正文

javaScript Vue2的高级用法

一.mixin的复用

        混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的选项。

var mixin = {
  data: function () {
    return {
      message: 'hello',
      foo: 'abc'
    }
  }
}

new Vue({
  mixins: [mixin],
  data: function () {
    return {
      message: 'goodbye',
      bar: 'def'
    }
  },
  created: function () {
    console.log(this.$data)
    // => { message: "goodbye", foo: "abc", bar: "def" }
  }
})

注意:同名钩子函数将合并为一个数组,因此都将被调用。另外,混入对象的钩子将在组件自身钩子之前调用。

值为对象的选项,例如 methodscomponentsdirectives,将被合并为同一个对象。两个对象键名冲突时,取组件对象的键值对。

1.1 全局混入

        混入也可以进行全局注册。使用时格外小心!一旦使用全局混入,它将影响每一个之后创建的 Vue 实例。使用恰当时,这可以用来为自定义选项注入处理逻辑。

// 为自定义的选项 'myOption' 注入一个处理器。
Vue.mixin({
  created: function () {
    var myOption = this.$options.myOption
    if (myOption) {
      console.log(myOption)
    }
  }
})

new Vue({
  myOption: 'hello!'
})
// => "hello!"

二.插槽

2.1默认插槽

我们在编写代码时,组件内部并不清楚这块内容的具体实现,我就需要将这个坑位留出,需要开发者传进来。

<div class="container">
  <main>
    <slot></slot>
  </main>
</div>

2.2具名插槽

带有名称的插槽,用于接收父组件中明确指定插槽名称的内容。

<div class="container">
  <header>
    <slot name="header"></slot>
  </header>
</div>

2.3作用域插槽

一种特殊的插槽,允许子组件将数据暴露给父组件的插槽内容。

子组件:

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      <slot name="item" :item="item">
        <!-- 后备内容 -->
        {{ item.text }}
      </slot>
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: '苹果' },
        { id: 2, text: '香蕉' },
        { id: 3, text: '橙子' }
      ]
    }
  }
}
</script>

父组件:

<template>
  <ScopedSlotChild>
    <template v-slot:item="slotProps">
      <!-- 使用slotProps访问子组件传递的数据 -->
      <strong>{{ slotProps.item.text }}</strong>
    </template>
  </ScopedSlotChild>
</template>

<script>
import ScopedSlotChild from './ScopedSlotChild.vue';

export default {
  components: {
    ScopedSlotChild
  }
}
</script>

三.插件

        插件可以是对象,或者是一个函数。如果是对象,那么对象中需要提供 install 函数,如果是函数,形态需要跟前面提到的 install 函数保持一致。

install 是组件安装的一个方法,跟 npm install 完全不一样,npm install 是一个命令

const MyPlugin = {
    install(Vue, options) {
      // 1. 添加全局方法或 property
      Vue.myGlobalMethod = function () {
        // 逻辑...
      }
    
      // 2. 添加全局资源
      Vue.directive('my-directive', {
        bind (el, binding, vnode, oldVnode) {
          // 逻辑...
        }
        ...
      })
    
      // 3. 注入组件选项
      Vue.mixin({
        created: function () {
          // 逻辑...
        }
        ...
      })
    
      // 4. 添加实例方法
      Vue.prototype.$myMethod = function (methodOptions) {
        // 逻辑...
      }
    }
};

使用插件:

Vue.use(MyPlugin);

{{ $myMethod }}

http://www.dtcms.com/a/99616.html

相关文章:

  • 微型导轨和普通导轨有哪些区别?
  • 如何在ms-swift 微调训练deepseekvl2时使用sageattention
  • flutter优秀项目推荐
  • 【Spring Boot 与 Spring Cloud 深度 Mape 之五】微服务守门神:Spring Cloud Gateway 核心详解与实战
  • Linux下xl9535 gpio扩展芯片bug调试
  • Java面试黄金宝典16
  • C语言_数据结构_排序
  • LeetCode 每日一题 2025/3/24-2025/3/30
  • Typora使用Gitee作为图床
  • Windows模仿Mac大小写切换, 中英文切换
  • Python自动化面试通关秘籍
  • 相似度计算 ccf-csp 2024-2-2
  • 网络华为HCIA+HCIP ip-prefix,route-policy
  • DBeaver Error : Public Key Retrieval is not allowed
  • 可视化图解算法: 二叉树的前序遍历
  • 算法-前缀和与差分
  • 【hadoop】远程调试环境
  • 用Python打造智能宠物:强化学习的奇妙之旅
  • 计算机三级信息安全部分英文缩写
  • 【MyBatis】MyBatis 操作数据库
  • Windows学习笔记(4)关于MITRE
  • 解决 FFmpeg 使用 C/C++ 接口时,解码没有 shell 快的问题(使用多线程)
  • 用Python实现资本资产定价模型(CAPM)
  • ubuntu 安装mysql
  • Python 中列表(List)、元组(Tuple)、集合(Set)和字典(Dict)四大数据结构的完整对比
  • macOS Jdk1.8安装(目前主流版本的jdk)
  • 【漫话机器学习系列】163.方差膨胀因子(Variance Inflation Factor, VIF)
  • Spring 通过多种方式实现使用线程
  • 在用redis当中可能遇到的问题解决方案以及redis中的一些名词解释
  • HTML 标签类型全面介绍