我遇到了问题,Vue将number类型的输入字段的值转换为字符串,我无法弄清楚原因.我正在遵循的指南没有遇到这个问题,正如预期的那样得到值作为数字.
vue docs声明,如果输入的类型是数字,Vue会将值转换为数字.
该代码是源于一个组成部分,但我调整它在运行的jsfiddle:https://jsfiddle.net/d5wLsnvp/3/
<template>
<div class="col-sm-6 col-md-4">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">
{{ stock.name }}
<small>(Price: {{ stock.price }})</small>
</h3>
</div>
<div class="panel-body">
<div class="pull-left">
<input type="number" class="form-control" placeholder="Quantity" v-model="quantity"/>
</div>
<div class="pull-right">
<button class="btn btn-success" @click="buyStock">Buy</button>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: ['stock'],
data() {
return {
quantity: 0 // Init with 0 stays a number
};
},
methods: {
buyStock() {
const order = {
stockId: this.stock.id,
stockPrice: this.stock.price,
quantity: …
Run Code Online (Sandbox Code Playgroud)