vue+ts 错误随笔

笔记

  • 问题
    • [Parameter 'xxx' implicitly has an 'any' type的解决](#Parameter ‘xxx’ implicitly has an ‘any’ type的解决)
    • [ts ref定义数组,数组遍历报错=>类型"never"上不存在属性"xx"](#ts ref定义数组,数组遍历报错=>类型“never”上不存在属性“xx”)
    • ts项目computed定义数据
    • [Getting a value from the `props` in root scope of `<script setup>` will cause the value to lose reactivity.](#Getting a value from the props in root scope of <script setup> will cause the value to lose reactivity.)
    • 函数-可选参数
    • [一个函数没有返回值,此时在 TS 的类型中,应该使用 void 类型](#一个函数没有返回值,此时在 TS 的类型中,应该使用 void 类型)
    • vue3+ts+setup,一键返回顶部

问题

Parameter 'xxx' implicitly has an 'any' type的解决

这句话翻译过来就是参数暗指是any类型

解决:tsconfig.json添加"noImplicitAny": false,

或者 "strict": true,改为false

javascript 复制代码
{
  "compilerOptions": {
    "strict": false, //<---或者修改这个
    "noImplicitAny": false // <-----
  }
}

ts ref定义数组,数组遍历报错=>类型"never"上不存在属性"xx"

javascript 复制代码
const list= ref<transferListItem[]>([]);
//原先是ref([]);报错的,改成上面就行了

ts项目computed定义数据

javascript 复制代码
//重点WritableComputedRef
const data:WritableComputedRef<transferListItem[]>   = computed({
      get() {
        return leftData.value
      },
      set(newValue) {
        leftData.value=newValue
      }
  })
    import { computed, type WritableComputedRef } from 'vue'
  
  // 验证通过
 const textvalue: WritableComputedRef<string> = computed({
    get() {
      return store.textvalue
    },
    set(newValue: string) {
      return store.changeTextvalue(newValue)
    }
  })

Getting a value from the props in root scope of <script setup> will cause the value to lose reactivity.

报错内容翻译为 从' setup() '的根范围的' props '中获取一个值将导致该值失去反应性

这样翻译下来应该就能明白什么意思了,就是我们从props中获取父组件传递数据的时候,不管父组件传递的是不是响应式的数据,在接收的时候要把数据转换为响应式的

javascript 复制代码
//改为这种格式即可
 const {option } = {...props }
 //举个例子:
let {
  leftBtnText,
  rightBtnText,
  leftTitleText,
  rightTitleText,
  placeholderLeft,
  placeholderRight } = { ...props}

函数-可选参数

javascript 复制代码
 // 可选参数:在可选参数名的后面添加 ?(问号)
function mySlice(start: number, end?: number): void {
  console.log('起始索引:', start, '结束索引:', end)
}

一个函数没有返回值,此时在 TS 的类型中,应该使用 void 类型

  • 不写return
  • 写return ,但是后面不接内容
  • 写return undefined
javascript 复制代码
// 如果什么都不写,此时,add 函数的返回值类型为: void
const add = () => {}
 
// 这种写法是明确指定函数返回值类型为 void,与上面不指定返回值类型相同
const add = (): void => {}
​
// 如果指定 返回值类型为 undefined,此时,函数体中必须显示的 return undefined 才可以
const add = (): undefined => {
  // 此处返回的 undefined 是 JS 中的一个值
  return undefined
}

vue3+ts+setup,一键返回顶部

javascript 复制代码
 <div class="anchor-point wh" ref="wrapper" @scroll="onScroll">
 </div>
 //返回到顶部
  function backTo(){
    document.querySelector('.anchor-point').scrollTo({
        top:0,
        behavior:'smooth'
      })
  }
相关推荐
麦麦麦当劳大王10 小时前
【油猴脚本】Boss直聘 岗位职责复制
开发语言·前端·javascript
只睡四小时11 小时前
Three.js Sub-tick:漏判 60.7%→0
开发语言·javascript·性能优化·ecmascript·游戏开发·three.js·碰撞检测
excel12 小时前
基于 GPT 的自动发布文章接口方案:从一句话或一张图片生成并发布内容
前端·javascript·后端
OpsEye17 小时前
大模型 Token 计量不准,是什么原因,怎么解决??
javascript·ai编程
独隅19 小时前
WebStorm Windows 版 React / Next.js 实战替换指南
javascript·windows·webstorm
冷眸_19 小时前
同名食材不能直接相减:一个 JavaScript 用量模型的边界
javascript
ruanno_top19 小时前
倒计时该用 deadline 还是每秒减一?我把两种写法都跑了一遍
vue.js
apollo_qwe19 小时前
接口超时了,前端为什么不能直接重试?
javascript·http
徐小夕1 天前
下载量超 30 万的开源智能体 Prime Agent:它把自己从 TypeScript 重写成了 Rust
前端·vue.js·github
toooooop81 天前
CRMEB 故障排查笔记:安全网关只放行 GET/POST 怎么办
前端·javascript·vue.js·thinkphp6·crmeb