企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 挂载Vue Prototype使用全局变量方法 需要做[模块补充](https://cn.vuejs.org/v2/guide/typescript.html?) ```js // 定义全局变量 declare module 'vue/types/vue' { interface Vue { service: any //service 是自定义的服务接口,挂载在Vue.prototype上 } } ``` 类似的,我们在使用vue-router,element-ui中的$message,$model时,同样需要增强类型。 ``` declare module 'vue/types/vue' { interface Vue { $Message: any, $Modal: any } } ``` ### 使用element-ui中validate进行表单校验 原先写法 ```js this.$refs.form.validate(valid=>{ if(valid){ // pass } }) ``` 在typescript中采用如上的写法,则抛出: ```js Property 'validate' does not exist on type 'Vue'. ``` 原因是未对`this.$refs.form`做类型声明: ```js const el: any = this.$refs.form el.validate((valid: Boolean) => { if (valid) { } }) ```