Vue 3 + TypeScript 项目中全局挂载并使用工具函数

一、proxy方式

1.封装日期选择工具函数:

在untils文件夹下新建index.ts,并导出工具函数

/**
 * 获取不同类型日期
 * param:类型 dateVal: 是否指定
 */
export function getSystemDate(param: any, dateVal: any) {
  let systemDate = dateVal ? new Date(dateVal) : new Date(),
    year = systemDate.getFullYear(),
    month = systemDate.getMonth() + 1,
    date = systemDate.getDate(),
    hours = systemDate.getHours(),
    minutes = systemDate.getMinutes(),
    seconds = systemDate.getDate(),
    milliseconds = systemDate.getMilliseconds();
  month = month < 10 ? "0" + month : month;
  date = date < 10 ? "0" + date : date;
  hours 
可以使用以下步骤在 Vue3 + Vite3 + Typescript使用 WangEditor 编辑器: 1. 安装 WangEditor。可以使用 npm 或 yarn 安装。 ```bash npm install wangeditor --save # 或者 yarn add wangeditor ``` 2. 在 `main.ts` 中引入 WangEditor 和 CSS 文件。 ```typescript import WangEditor from &#39;wangeditor&#39;; import &#39;wangeditor/release/wangEditor.css&#39;; const app = createApp(App); app.config.globalProperties.$WangEditor = WangEditor; // 挂载编辑器到全局 app.mount(&#39;#app&#39;); ``` 3. 在组件中使用 WangEditor。 ```vue <template> <div class="editor-wrapper"> <div ref="editorRef"></div> </div> </template> <script lang="ts"> import { defineComponent, onMounted, ref } from &#39;vue&#39;; export default defineComponent({ name: &#39;Editor&#39;, setup() { const editorRef = ref<HTMLDivElement>(); onMounted(() => { const editor = new (window as any).$WangEditor(editorRef.value); editor.create(); }); return { editorRef, }; }, }); </script> <style lang="scss"> .editor-wrapper { height: 400px; .w-e-text-container { height: 100%; } } </style> ``` 在 `onMounted` 钩子函数中,使用 `new (window as any).$WangEditor` 来创建编辑器实例,传入编辑器容器的 DOM 节点。调用 `editor.create()` 方法来创建编辑器。 注意:由于 WangEditor 的类型定义文件不完善,因此可以在 `tsconfig.json` 中添加以下配置来避免类型检查报错。 ```json { "compilerOptions": { "skipLibCheck": true } } ``` 这样,就可以在 Vue3 + Vite3 + Typescript使用 WangEditor 编辑器了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尔嵘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值