【问题标题】:Vue.js + Element UI - How to change value el-switchVue.js + Element UI - 如何更改值 el-switch
【发布时间】:2018-05-22 04:44:51
【问题描述】:

我有来自服务器的数据。响应数据为整数 1 或 0,鉴于我使用元素 ui el-switch,它使用布尔值 true 或 false 设置值。

有什么可以帮助我的吗? 这是我的代码

<div class="row">
          <el-col :span="9">
            <el-form-item>
              <p style="margin-top: 15px">sale in POS</p>
            </el-form-item>
          </el-col>
          <el-col :span="15" class="switch-varian">
            <el-switch inactive-color="#ff4949" v-model="product.pos_hidden" @change="valuePOS"></el-switch>
            <div v-if="product.pos_hidden === 1">
              <p class="desc-switch">
                <strong>Yes</strong>
              </p>
              <p class="desc-switch">This Product Sale in POS</p>
            </div>
            <div v-else>
              <p class="desc-switch">
                <strong>No</strong>
              </p>
              <p>This Product Not Sale in POS</p>
            </div>
          </el-col>
        </div>

methods: {
loadData() {
  this.loading = true
  let headers = {
    Authorization: 'Bearer ' + window.accessToken
  }

  axios({
    method: 'GET',
    url: BASE_API + 'product/' + this.$route.params.id,
    params: this.params,
    headers: headers
  })
    .then(response => {
      this.product = response.data.data
      this.loading = false
    })
    .catch(error => {
      console.log(error)
      this.loading = true
      this.$notify({
        title: 'Error: ' + error,
        type: 'warning',
        message: 'Error: ' + error
      })
    })
},
valuePOS(val) {
  if (val === true) {
    fooBar | 1
  } else {
    fooBar | 0
  }
}

预期: 我想将值从响应更改为布尔值。 然后我将编辑该值并将新值作为整数值发送到服务器。

【问题讨论】:

    标签: javascript vuejs2 parseint element-ui


    【解决方案1】:

    您可以使用active-valueinactive-valueDocument

      <el-switch
        v-model="product.pos_hidden"
        :active-value="1"
        :inactive-value="0">
      </el-switch>
    

    【讨论】:

    • 我尝试过,但该值是字符串而不是整数,它是来自服务器的响应 (pos_hidden:0),它来自值更改 (pos_hidden:"1")
    • 我已经更新了,你可以绑定值使其变成整数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2018-06-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多