Vue 中使用 extend 开发组件

  • 简介:再开发过程中那面会遇到自定义 loading alert 等全局组件,这里我们可以使用 vue 中的extend 来帮助我们完成

  • 一个简单extend例子,如下:

  • 新建 index.js 文件

import Vue from "vue";
import Index from "./index.vue"; const MessageBoxConstructor = Vue.extend(Index);
const defaultInstanceCallback = action => {
console.log('---------')
}
let instance;
const initInstance = (): void => {
instance = new MessageBoxConstructor({
el: document.createElement('div')
})
instance.$mount();
document.body.appendChild(instance.$el);
return instance;
} const UseMessageBox = function (): void {
if (!instance) {
initInstance();
}
} UseMessageBox.success = () => {
UseMessageBox();
console.log('---------------', instance);
instance.showClose = false;
} export default UseMessageBox;
  • 在建一个index.vue 文件 文件内容如下
<template>
<div v-if="showClose" style=" color: #fff;position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0,0,0, .7); z-index: 9999">
MessageBox 弹
</div>
</template>
<script lang="ts">
import {defineComponent, ref} from "@vue/composition-api";
export default defineComponent({
name: 'UseMessageBox',
setup(props, _this) {
const showClose = ref<boolean>(false);
return {
showClose,
}
}
})
</script> <style lang="scss">
@import "index";
</style>
  • 调用方法 页面中引入 UseMessageBox
UseMessageBox.success()
  • 补充一下在开发过程中碰到了一个问题内容如下
 No overload matches this call.
The last overload gave the following error.
Argument of type 'VueProxy<unknown, { showClose: Ref<boolean | undefined>; options: Ref<{ title?: string | undefined; message?: string | undefined; cancel?: any; submit?: any; isShowCancel?: boolean | undefined; }>; ... 5 more ...; submit: () => void; }, Data, {}, {}>' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<...>> | undefined'.
Type 'VueProxy<unknown, { showClose: Ref<boolean | undefined>; options: Ref<{ title?: string | undefined; message?: string | undefined; cancel?: any; submit?: any; isShowCancel?: boolean | undefined; }>; ... 5 more ...; submit: () => void; }, Data, {}, {}>' is not assignable to type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<...>>'.
Types of property 'props' are incompatible.
Type 'unknown' is not assignable to type 'ArrayPropsDefinition<Record<string, any>> | RecordPropsDefinition<Record<string, any>> | undefined'.
Type 'unknown' is not assignable to type 'RecordPropsDefinition<Record<string, any>>'. 没有与此调用匹配的重载。 上一次重载导致以下错误。 类型为“VueProxy<unknown,{showClose:Ref<boolean | undefined>”的参数;选项:Ref<{title?:string | undefined;message?:string | undefined;cancel?:any;submit?:boolean | undefined;}>…5更多…;submit:=>void;},Data,{},{}>”不能分配给类型为“ComponentOptions”的参数,DefaultMethods<Vue>,DefaultComputed,PropsDefinition<Record<string,any>>,Record<…>|undefined'。 键入“VueProxy<unknown,{showClose:Ref<boolean | undefined>;选项:Ref<{title?:string | undefined;message?:string | undefined;cancel?:any;isshowconcel?:boolean | undefined;}5更多…;submit:()=>void;},Data,{},{}>“不可分配给类型”ComponentOptions<Vue,DefaultData<Vue>,DefaultData<Vue>,DefaultMethods<Vue>,DefaultComputed,PropsDefinition<Record<string,any>>,Record<…>>。 属性“props”的类型不兼容。 类型“unknown”不可分配给类型“ArrayPropsDefinition<Record<string,any>>| RecordPropsDefinition<Record<string,any>>| undefined”。 类型“未知”不能分配给类型“RecordPropsDefinition<Record<string,any>>”。
  • 百度了一下,大概明白是什么意思,就是 ComponentOptions 这个的原因

    了解ComponentOptions

解决: 在代码中添加 props: {}

此内容后续待补充。。。

Vue 中使用 extent 开发loading等全局 组件的更多相关文章

  1. 循序渐进VUE+Element 前端应用开发(20)--- 使用组件封装简化界面代码

    VUE+Element 前端应用,比较不错的一点就是界面组件化,我们可以根据重用的指导方针,把界面内容拆分为各个不同的组合,每一个模块可以是一个组件,也可以是多个组件的综合体,而且这一个过程非常方便. ...

  2. Vue中Js动画 与Velocity.js 多组件多元素 列表过渡

    Vue提供我们很多js动画钩子 写在tansition标签内部 入场动画 @before-enter="" 处理函数收到一个参数(e l) el为这个元素 @enter=" ...

  3. vue中html模板使用绑定的全局函数

    我们知道在script中使用vue绑定的全局函数时, 我们需要用这种方式使用: this.Util.Fun(e) 那在模板中, 比如v-if中想使用Fun函数怎么办呢?你应该这样做 <i v-i ...

  4. vue中alert toast confirm loading 公用

    import Vue from 'vue' import { ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin } from 'vux' / ...

  5. vue中允许你继续使用swiper的组件 vue-awesome-swiper---切图网

    swiper是一个在切图中好用到不行的图片轮播插件,包括3d轮播.h5滑屏等复杂应用都不在话下,到了vue项目一切逻辑完全颠覆了,没有获取dom的概念,还好有 vue-awesome-swiper组件 ...

  6. vue中.sync修饰符,实现子组件实时更新父组件的值

    vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定. 不过它有一个前身,先来看看.sync出现之前是如何实现的 父组件中(传递给子组件一个值:p ...

  7. 在vue中结合render函数渲染指定的组件到容器中

    1.demo 项目结构: index.html <!DOCTYPE html> <html> <head> <title>标题</title> ...

  8. vue中的路由传参及跨组件传参

    路由跳转   this.$router.push('/course'); this.$router.push({name: course}); this.$router.go(-1); this.$r ...

  9. vue中多个元素或多个组件之间的动画效果

    多个元素的过渡 <style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-acitve,.v-leave-active{ opacity: ...

随机推荐

  1. 初始HTML03

    **------------恢复内容开始------------** HTML 页面标签组成 一个完整的页面仅有一个html元素,在这个元素之下,包含head和body元素,前者负责说明页面结构,后者 ...

  2. MAC 安装 apache ab 压力测试工具以及遇到的坑

    ab 是apache对 http服务器进行压力测试的工具,它可以测试出服务器每秒可以处理多少请求.本文记录mac版本安装 ab 的步骤以及遇到的坑. 下载 进入 apache ab官网 下载页面. 安 ...

  3. Hadoop MapReduce 保姆级吐血宝典,学习与面试必读此文!

    Hadoop 涉及的知识点如下图所示,本文将逐一讲解: 本文档参考了关于 Hadoop 的官网及其他众多资料整理而成,为了整洁的排版及舒适的阅读,对于模糊不清晰的图片及黑白图片进行重新绘制成了高清彩图 ...

  4. HTML中的emment

    emment的优点和使用方法: 优点:Emment是前端开发者必备的工具,使用它可以大大提高前端开发效率. 使用方法:Emment的使用方法非常简单,直接在编辑器上输入HTML和CSS代码的缩写,然后 ...

  5. [no_code][Beta]项目展示博客

    $( "#cnblogs_post_body" ).catalog() 团队项目链接 Beta阶段核心开发点: github 前端 github 后端 github OCR文档-含 ...

  6. 使用json-path解析json

    在我们的日常开发中,有时候需要从一个json字符串中获取一个值,或者从一段json字符串中获取到某些值,如果先使用Gson或Jackson转换成java对象在获取值,有些时候是很麻烦的,那么有没有一种 ...

  7. 021中国大学生程序设计竞赛(CCPC)- 压力测试赛题解

    A.Matrix 挺狗的一道题,从开始冲到最后都没冲出来,都没啥思路. 其实分开考虑每个数的贡献,这个想法也存在过,就是不知道该怎么计算,我们考虑我们单独考虑一个数字\(i(1\leq i\leq n ...

  8. poj 1330 Nearest Common Ancestors (最简单的LCA)

    题意: 给出一棵树的结构. 给出两个点X和Y,求它俩的LCA. 思路: 只需求两个点的LCA,用了两种方法,一种离线tarjan,一种直接搞. 看代码. 代码: 方法一:直接搞. int const ...

  9. 20191310Lee_yellow缓冲区溢出实验

    缓冲区溢出实验 1.什么是缓冲区溢出 ​ 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据 ...

  10. 单自由度系统中质量、阻尼和刚度变化对频率响应函数(FRF)影响图的绘制

    作者:赵兵 日期:2020-02-17 目录 单自由度系统中质量.阻尼和刚度变化对频率响应函数(FRF)影响图的绘制 1.     背景 2.     VISIO绘制 3.     Matlab绘制 ...