自学内容网 自学内容网

uniapp插件uViewplus的使用(涉及TS下的问题)

在技术选型后最后定了使用有团队维护的uview-plus3.0,官方文档
配置参考:https://juejin.cn/post/7169875753100640270
ts配置参考:https://blog.csdn.net/m0_70027114/article/details/132957426

引入

在项目文件的pages.json中的"easycom".“custom”: 添加如下

"easycom": {
"autoscan": true,
"custom": {
……
"^u--(.*)": "@/uni_modules/uview-plus/components/u-$1/u-$1.vue",
"^up-(.*)": "@/uni_modules/uview-plus/components/u-$1/u-$1.vue",
"^u-([^-].*)": "@/uni_modules/uview-plus/components/u-$1/u-$1.vue"
}
},

main.js文件中引入插件

// 引入全局uview-plus
 import uviewPlus from './uni_modules/uview-plus/index.js'

 const app = createSSRApp(App)
 app.use(uviewPlus)
 
 return {
  app
 }

TS相关
创建env.d.ts

declare module '@/uni_modules/uview-plus';

并写入tsconfig.json

{
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"typeRoots": [
"@/env.d.ts"
]
}

遇到的问题

我的vue3项目使用了ts,这个插件引入之后就开始报uni. u 属性不存在,但不影响运行。因为 u属性不存在,但不影响运行。 因为 u属性不存在,但不影响运行。因为u认不出来,他和下面的属性没有提示,书写起来不方便,容易写错。
参考了两个官方论坛的解决方案
https://ask.dcloud.net.cn/question/163482
https://ask.dcloud.net.cn/question/141695
编译器HBuilder3.99 是允许的
https://www.cnblogs.com/00544–/p/17097648.html

declare module 'uview-plus' {  
  export function install(): void  //必要
  interface test {  
    /** 邮箱格式校验 */  
    email(email: string): boolean  
  }  
  interface $u {  
    test: test  
  }  

  global {  
    interface Uni {  
      $u: $u  
    }  
  }  
}

猜测需要将所有用到的类型一一注册,才能作为属性调用,uView的属性还挺多的,工程量有点大就没写


原文地址:https://blog.csdn.net/yuemitengfeng/article/details/136194288

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!