vue3+ts 打包报错:error TS2339: Property ‘$urlLink‘ does not exist on type ‘{ $: ComponentInternalInstanc

vue3+ts 打包报错:error TS2339: Property ‘$urlLink’ does not exist on type '{ $: ComponentInternalInstanc

部分报错信息: error TS2339: Property ‘$urlLink’ does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: { key?: string | number | symbol | undefined; ref?: VNodeRef | undefined; ref_for?: boolean | undefined; … 8 more …; style?: unknown; }; … 10 more …;

描述:这一般是在main.ts中定义了全局变量,并且在页面中调用后出现了红线信息提示,最后打包后有上面的错误提示

解决方案:

方案一:如果是main.ts是如下定义:

app.config.globalProperties.$urlLink = 'http://xxxxx/static/home/images/'

添加一个globalProperties.d.ts文件,和main.ts平级,代码如下:

export {
    
     }  //这个一定要加,不加表示覆盖原来的类型定义
declare module 'vue' {
    
    
	interface ComponentCustomProperties {
    
    
		$urlLink: string
	}
}

方案二:如果是main.ts是如下定义:

// 线上图片路径
app.config.globalProperties.$urlLink = (key: string) => {
    
    
  return `http://xxxxx/static/home/images/${
      
      key}`;
};

添加一个globalProperties.d.ts文件,和main.ts平级,代码如下:

export {
    
     }  //这个一定要加,不加表示覆盖原来的类型定义
declare module 'vue' {
    
    
	interface ComponentCustomProperties {
    
    
		$urlLink: (key: string) => string
	}
}

上述操作完之后就不会报错可以打包了!!!

猜你喜欢

转载自blog.csdn.net/heavenz19/article/details/130961825
今日推荐