【问题标题】:vue composition api toRefs typescriptvue composition api toRefs typescript
【发布时间】:2021-02-15 04:04:46
【问题描述】:

在为 props 创建 ref 时如何获取正确的类型?我试过这样

setup(props) {
    const { userId } = toRefs(props); // userId is string
    retrun {
        ...getComposition(userId);
    };
}

...
function getComposition(userId) {
    // How to make sure that userId.value is string?)
    const caps = userId.value.toUperCase(); // here how to get string method hints
}

【问题讨论】:

    标签: typescript vue-composition-api


    【解决方案1】:

    https://v3.vuejs.org/guide/typescript-support.html#using-with-composition-api

    Refs 从初始值推断类型:

    但我认为 IDE 可能会像我们一样感到困惑。无论如何,这在 webstorm 中对我有用...

      setup (props) {
        const { userId } = toRefs(props); // userId is string
        function getComposition(idRef) {
          return {caps: (idRef.value as string).toUpperCase()};
        }
        return {
          ...getComposition(userId)
        };
      }
    

    vue devtools

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 2020-11-11
      • 2020-04-10
      • 2022-11-29
      • 2021-01-08
      • 2020-06-16
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多