-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 init: 初始化组件库的install配置和tsconfig.json配置;
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!-- | ||
在该组件中简单的定义了组件名、代理了一下v-model,并暴露出了一个方法focus。 | ||
--> | ||
|
||
<template> | ||
<div class="gie-input"> | ||
<input v-model="state" ref="inputRef" class="gie-input__control" type="text" :disabled="props.disabled" /> | ||
</div> | ||
</template> | ||
<script setup lang="ts"> | ||
import { computed, ref } from "vue"; | ||
import type { InputEmits, InputProps } from "./Input"; | ||
|
||
defineOptions({ | ||
name: "GieInput", | ||
}); | ||
|
||
const emit = defineEmits<InputEmits>(); | ||
|
||
const props = withDefaults(defineProps<InputProps>(), { | ||
modelValue: "", | ||
disabled: false, | ||
}); | ||
|
||
const state = computed({ | ||
get: () => props.modelValue, | ||
set: (val) => { | ||
emit("update:modelValue", val); | ||
}, | ||
}); | ||
|
||
const inputRef = ref<HTMLInputElement>(); | ||
|
||
function focus() { | ||
inputRef.value?.focus(); | ||
} | ||
|
||
defineExpose({ | ||
focus, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* index.ts — 定义Input组件的入口文件 | ||
* | ||
* 在入口文件中,使用withInstall封装了一下导入的Input组件,并默认导出。 | ||
* | ||
* 且在下面导出了所有类型文件。 | ||
* | ||
* 这个withInstall函数的作用就是把组件封装成了一个可被安装,带install方法的vue插件, | ||
* 这个函数我是直接从element-plus项目复制的😂。 | ||
* | ||
* typescript 代码解读复制代码 | ||
*/ | ||
|
||
import { withInstall } from "../utils/install"; | ||
|
||
import Input from "./Input.vue"; | ||
|
||
export const GieInput = withInstall(Input); | ||
export default GieInput; | ||
|
||
export * from "./Input.vue"; | ||
export * from "./Input"; |
16 changes: 16 additions & 0 deletions
16
learn-create-compoents-lib/components/src/utils/install.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { App, Plugin } from "vue"; | ||
export type SFCWithInstall<T> = T & Plugin; | ||
export const withInstall = <T, E extends Record<string, any>>(main: T, extra?: E) => { | ||
(main as SFCWithInstall<T>).install = (app): void => { | ||
for (const comp of [main, ...Object.values(extra ?? {})]) { | ||
app.component(comp.name, comp); | ||
} | ||
}; | ||
|
||
if (extra) { | ||
for (const [key, comp] of Object.entries(extra)) { | ||
(main as any)[key] = comp; | ||
} | ||
} | ||
return main as SFCWithInstall<T> & E; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
// "extends": "../../tsconfig.json", | ||
// 在我们这个项目内,其目录结构有变化,换成别的。 | ||
// 换成vite项目自主新建的tsconfig.json | ||
"extends": "../tsconfig.json", | ||
"include": [ | ||
"src" | ||
], | ||
"compilerOptions": { | ||
"moduleResolution": "node", | ||
"baseUrl": "." | ||
} | ||
} |