1、vue2.5版本,创建.d.ts文件
	import Vue from 'vue'	确保在声明补充的类型之前导入'vue'
	
	(1)为实例和类添加额外约束
		declare module 'vue/types/vue' {	在types/vue.d.ts里Vue有构造函数类型
		  interface Vue {	声明合并,会添加到类的原型上
		    $myProperty: string	
		  }
		  
		  interface VueConstructor {	声明全局属性,即Vue.$myGlobal
		    $myGlobal: string
		  }
		}
	
	(2)为组件属性添加额外约束
		declare module 'vue/types/options' {
		  interface ComponentOptions<V extends Vue> {	声明组件选项的类型
		    myOption?: string
		  }
		}
			即:
				var vm = new Vue({
				  myOption: 'Hello'
				})

2、vue3.0版本
	(1)声明往Vue实例上添加的属性
		方式一:
			在main.ts,createApp之前添加
				declare module '@vue/runtime-core' {
				  interface ComponentCustomProperties {
				    foo:string
				  }
				}
		方式二:
			在src下创建x.d.ts文件
			declare module '@vue/runtime-core' {
			  interface ComponentCustomProperties {
			    foo:string
			  }
			}
			export {}   这里需要export/export default任意内容,暂未知原因
Logo

前往低代码交流专区

更多推荐