【问题标题】:How to pass object instead of string in Nativescript-vue with typescript?如何使用打字稿在 Nativescript-vue 中传递对象而不是字符串?
【发布时间】:2020-03-16 17:31:13
【问题描述】:

我想在{N} Playground Code 中实现 PromptDialog

import dialogs from "tns-core-modules/ui/dialogs";
 
  export default {
    methods: {
            onButtonTap() {
                console.log("Button was pressed");
                prompt({
                    title: "Email Prompt",
                    message: "Provide your email address:",
                    okButtonText: "OK",
                    cancelButtonText: "Cancel",
                    defaultText: "name@domain.com",
                    inputType: dialogs.inputType.email
                }).then(result => {
                    console.log(`Dialog result: ${result.result},
                text: ${result.text}`);
                });
            }
        },
  }

但是 Vetur 给出了以下 Typescript 错误。

Argument of type '{ title: string; message: string; okButtonText: string; cancelButtonText: string; defaultText: string; inputType: string; }' is not assignable to parameter of type 'string'.

这只是nativescript-vue prompt documentation 中的示例代码,但打字稿适应。

基本使用

    forgotPassword() {
      prompt('Your message to the user', 'Suggested user input').then(result => {
        console.log(`Dialog result: ${result.result}, text: ${result.text}`);
      });
    }

Vetur 错误:

Property 'then' does not exist on type 'string'.

【问题讨论】:

标签: typescript vue.js nativescript nativescript-vue


【解决方案1】:

这是一个错误的文档引导。没有称为 prompt 的 API。它应该是 dialogs.prompt。 正确的例子

<script lang="ts">
    import * as dialogs from "tns-core-modules/ui/dialogs";
     export default {
        methods: {
            onButtonTap() {
                console.log("Button was pressed");
                dialogs.prompt({
                    title: "Email Prompt",
                    message: "Provide your email address:",
                    okButtonText: "OK",
                    cancelButtonText: "Cancel",
                    defaultText: "name@domain.com",
                    inputType: dialogs.inputType.email
                }).then(result => {
                    console.log(`Dialog result: ${result.result},
                text: ${result.text}`);
                });
            }
        },

        data() {
            return {};
        }
    };
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多