Skip to content

Commit 9e724d2

Browse files
committed
refactor: add types
1 parent 9947299 commit 9e724d2

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

example/src/main.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Vue from 'vue'
22
import App from './App.vue'
33
/* for testing */
4-
// import VueDynamicComponent from "../../dist/index";
5-
import VueDynamicComponent from "vuejs-dynamic-component";
4+
import VueDynamicComponent from "../../dist/index";
5+
// import VueDynamicComponent from "vuejs-dynamic-component";
66
import components from './config'
7-
Vue.use(new VueDynamicComponent(), components)
7+
Vue.use(new VueDynamicComponent(components))
88

99
Vue.config.productionTip = false
1010

src/index.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import _Vue, { PluginObject, Component, EventType } from './interface'
1+
import { VueType, PluginObject, Component, ComponentConfig, EventType } from './interface'
22

3-
class VueDynamicComponent implements PluginObject<Component> {
4-
private V: _Vue
3+
class VueDynamicComponent implements PluginObject<Component, any> {
4+
private V: VueType
55

6-
private components: Component
6+
components: Component
77

8-
constructor() {
8+
constructor(components: Component) {
99
this.V = null
10-
this.components = {}
10+
this.components = components
1111
}
1212

13-
public install(Vue, components) {
13+
public install(Vue: VueType): void {
1414
this.V = Vue
15-
this.components = components
1615
this.V.prototype.$bus = new Vue()
1716
this.containerInit(this.V)
1817
this.childComponentsRegister()
1918
}
2019

21-
private containerInit(V) {
20+
private containerInit(V: VueType): void {
2221
const container = {
2322
name: 'dynamic-container',
2423
data() {
@@ -67,7 +66,7 @@ class VueDynamicComponent implements PluginObject<Component> {
6766
document.body.appendChild(instance.$el)
6867
}
6968

70-
private createDynamicComp(component, { attrs, on, ...args }) {
69+
private createDynamicComp(component: VueType, { attrs, on, ...args }: ComponentConfig): VueType {
7170
const compInstance = this.V.extend(component)
7271
this.V.prototype.$bus.$emit(EventType.APPEND, compInstance, {
7372
attrs,
@@ -77,7 +76,7 @@ class VueDynamicComponent implements PluginObject<Component> {
7776
return compInstance
7877
}
7978

80-
private deleteDynamicComp(compInstance) {
79+
private deleteDynamicComp(compInstance: VueType) {
8180
this.V.prototype.$bus.$emit(EventType.REMOVE, compInstance)
8281
}
8382

@@ -89,7 +88,8 @@ class VueDynamicComponent implements PluginObject<Component> {
8988
// eslint-disable-next-line @typescript-eslint/no-use-before-define
9089
this.deleteDynamicComp(componentInstance)
9190
}
92-
const componentInstance = this.createDynamicComp(this.components[key], {
91+
const instance = this.components[key]
92+
const componentInstance = this.createDynamicComp(instance, {
9393
attrs,
9494
on: {
9595
'on-confirm': data => {

src/interface.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import _Vue from 'vue'
22

3-
export default _Vue
4-
5-
type PluginFunction<T> = (Vue: typeof _Vue, components?: T) => void
6-
7-
export interface PluginObject<T> {
8-
install: PluginFunction<T>
3+
export type VueType = typeof _Vue
4+
export type PluginFunction<U> = (Vue: VueType, options?: U) => void
5+
export interface ComponentConfig {
6+
attrs: any
7+
on: any
98
[key: string]: any
109
}
10+
export interface PluginObject<T, U> {
11+
// V: VueType
12+
components: T
13+
install: PluginFunction<U>
14+
// containerInit: (V: VueType) => void
15+
// createDynamicComp: (component: VueType, config: ComponentConfig) => VueType
16+
// deleteDynamicComp: (compInstance: VueType) => void
17+
// childComponentsRegister: () => void
18+
}
1119

1220
export interface Component {
13-
[key: string]: typeof _Vue
21+
[key: string]: VueType
1422
}
1523

1624
export enum EventType {

0 commit comments

Comments
 (0)