【问题标题】:Empty textfield after button click nativescript vue按钮单击nativescript vue后的空文本字段
【发布时间】:2020-11-23 12:03:43
【问题描述】:

我在发送填写的消息后尝试清空一个文本字段。消息被发送到数据库,但之后它并没有清空它。

文本字段:

<FlexboxLayout class="text-send">
      <TextField
        v-model="newMessage"
        id="try"
        class="body text-field"
        width="70%"
        @returnPress="onSendMessage()"
      ></TextField>

      <Button
        class="btn-secondary btn-design btn-next fas"
        text.decode=">"
        width="10%"
        @tap="onSendMessage()"
      />
    </FlexboxLayout>

发送消息代码:

onSendMessage() {
  if (this.newMessage != "") {
    let currentDate = Date()
    firebase
      .push("/messages/" + this.chatId, {
        message: this.newMessage,
        sender: this.getUserID,
        timestamp: currentDate,
      })
      .then(function (result) {
        console.log("created key: " + result.key); 
      });
  }
  this.newMessage = ""
},

this.newMessage = "",应该清除数据,但是没有用。

【问题讨论】:

  • 尝试在你的.then中移动this.newMessage = ''或者在你清除后记录新消息的结果请进一步调查

标签: vue.js nativescript textfield


【解决方案1】:

.then() 中移动newMessage。您可以在newMessage 被清除后添加console.log 来尝试调试,看看输出是什么。

onSendMessage() {
  if (this.newMessage != "") {
    let currentDate = Date()
    firebase
      .push("/messages/" + this.chatId, {
        message: this.newMessage,
        sender: this.getUserID,
        timestamp: currentDate,
      })
      .then(function (result) {
        console.log("created key: " + result.key); 

        this.newMessage = ""

      });
  }
},

【讨论】:

  • 在 .then() 中无法调用它。什么都没有。如果我将console.log("new message: " + this.newMessage) 放在 then() 中,它不会显示在终端中。在 .then() 之外更新它时,它确实将 newMessage 更新为空,但不会更新页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
相关资源
最近更新 更多