vue.js - b-输入模型具有字符串类型的值

标签 vue.js bootstrap-vue

我正在尝试创建一个带有数字字段的表单。

<b-input v-model="testNumber" type="number"/>

在我的数据中,我有一个简单的数字变量。

data() {
  return {
    testNumber: 10,
  }
},

但是当我跟踪 testNumber 时它是一个字符串

{{ typeof testNumber }}  // String

最佳答案

您可以向 v-model 添加修饰符。

( https://v2.vuejs.org/v2/guide/forms.html#number )

<b-form-input v-model.number="testNumber" />

更新

不要使用 v-model.number这是 bootstrap-vue 建议不要这样做:

v-model modifiers .number and .trim can cause unexpected cursor jumps when the user is typing (this is a Vue issue with v-model on custom components). Avoid using these modifiers.

但按照 b-form-input 的建议使用:

To get around this, <b-form-input> and <b-form-textarea> have two boolean props trim and number which emulate the native Vue v-model modifiers .trim and .number respectively.

<b-form-input v-model="testNumber" :number="true" />

关于vue.js - b-输入模型具有字符串类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237702/

相关文章:

vuejs2 - Bootstrap-Vue <b-card/> 标题中带有链接

javascript - Vim:如何从行首计算缩进

javascript - 从目录中导入所有文件并将文件名映射到数组中

css - bootstrap-vue 改变 <b-modal> 的位置

laravel - 如何在 Laravel 中包含 Bootstrap-Vue

javascript - vuejs 根据条件选择选项更改

html - 全局覆盖 Vuetify 分隔线颜色

laravel - 停止在 Laravel 中渲染 Vue 组件

javascript - v-model 更改后如何触发函数?

vue.js - 如何使用 sass-loader 在 vue.config.js 中配置 Dart Sass native 可执行文件(dart VM)?