如何在打字稿中使用 ref ?

WoJ*_*WoJ 4 typescript vue.js vuejs3 vue-composition-api

我正在尝试学习 Vue3 + Typescript(到目前为止我用纯 JS 编写了 Vue2 应用程序)。我试图在以下位置定义一个反应变量setup()

\n
setup() {\n    // a single note\n    interface Note {\n      id: string\n      creationDate: string\n      text: string\n      tags: string[]\n      deleted: boolean\n    }\n    // all the notes in the app\n    let allNotes: ref(Note[])  // \xe2\x86\x90 this is not correct\n    let allnotes: Note[]  // \xe2\x86\x90 this is correct but not reactive\n    \n    (...)\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

创建反应式数组的正确语法是什么Note

\n

Bou*_*him 15

它应该放在<>

let allNotes= ref<Note[]>([]) 
Run Code Online (Sandbox Code Playgroud)

默认情况下,ref从初始值推断类型,例如

const name=ref('') //name is a type of string
Run Code Online (Sandbox Code Playgroud)

参考输入:

interface Ref<T> {
  value: T
}

function ref<T>(value: T): Ref<T>
Run Code Online (Sandbox Code Playgroud)