vue事件绑定问题(二)

来源:3-2 axios 封装 & 数据获取

天羽狩

2019-02-18

老师,您好,看了您的回复我试了一下改成监听 @change 事件,但是还是不能触发methods中的choseProduct函数,我把这个组件的所有代码贴给您,能麻烦您帮我看看是哪里的问题吗?谢谢。

<template>
  <div>
    <h1 class="title">请选择需要发布的产品线和项目</h1>
    <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo_ruleForm">
      <el-form-item label="请选择产品线">
        <el-select v-model="ruleForm2.value5" class="demo_product" placeholder="产品线">
          <el-option
            v-for="item in product"
            :key="item.value"
            :label="item.name"
            :value="item.name"
            @change="choseProduct(ruleForm2.value5)">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请选择项目">
        <el-select  v-model="ruleForm2.value7" class="demo_project" placeholder="项目">
          <el-option
            v-for="item in project"
            :key="item.value"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">发布</el-button>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { checkproductinfo, checkprojectinfo } from '../../utils/api'
  export default {
    data() {
      return {
        ruleForm2: {
          value5: [],
          value7: []
        },
        product: [],
        project: [],
        rules2: {
          product: [
            { required: true, message: '请选择产品线', trigger: 'change' }
          ],
          project: [
            { required: true, message: '请选择要发布的项目', trigger: 'change' }
          ]
        }
      }
    },
    mounted () {
      checkproductinfo().then((res) => {
        this.product = res.data
        console.log(this.product)
      })
      /* checkprojectinfo().then((res) => {
        this.project = res.data
      }) */
    },
    methods: {
      choseProduct(valuex) {
        checkprojectinfo().then((res) => {
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].name === valuex[0]) {
              this.project[i] = res.data[i]
            }
          }
          console.log('the new informations')
          console.log(this.project)
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm2.value5)
            console.log(this.ruleForm2.value7)
            alert('submit!')
          } else {
            console.log('error submit!!')
            return false
          }
        })
        /* addprojectinfo(this.ruleForm2).then(res => {
          console.log(this.ruleForm2.value5)
        }) */
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import url("//unpkg.com/element-ui@2.4.11/lib/theme-chalk/index.css");
  .title
    text-align: left;
    padding-left: 500px;
    padding-top: 100px;
  .demo_ruleForm
    padding-top: 50px;
    padding-left: 450px;
</style>

写回答

4回答

ustbhuangyi

2019-02-19

首先,我简单的修改了你一些代码
//img.mukewang.com/szimg/5c6b6d71000180c513040654.jpg

//img.mukewang.com/szimg/5c6b6d830001993f16121244.jpg

其次,你的接口设计有几处不合理的地方,我给你指出一下:
1. 无论切换到哪个选择框都是请求同一个接口,然后在返回值中做判断过滤,不合理,应该是请求带上产品线参数,返回的数据应该直接可以拿来用,而不用过滤;
2. 返回的数据,应该需要一个 id 字段,目前没有的,所以只能用 name 作为 key

0
1
天羽狩
谢谢老师的指点,我再优化下
2019-02-19
共1条回复

天羽狩

提问者

2019-02-19

//img.mukewang.com/szimg/5c6b69af0001d40204510362.jpg

老师,我把 @change 事件绑定在el-select组件上了,但是如图第二个选项框还是没有选择内容,console.log没有出现自定义的内容,应该是这个函数还是没有被触发。

0
1
ustbhuangyi
可以的啊
2019-02-19
共1条回复

ustbhuangyi

2019-02-19

//img.mukewang.com/szimg/5c6b5f4f000119e714040634.jpg
@change 事件是绑定在 el-select 组件上的

0
1
天羽狩
老师,还是不行,我添加了一条回答,直接回复不能上传截图。我后端的代码已经上传到公网服务器,接口全部是可以用的,老师如果有空的话可以运行一下试试?麻烦您了。
2019-02-19
共1条回复

ustbhuangyi

2019-02-18

你把代码传到 GitHub 上吧,我抽空看看

0
1
天羽狩
https://github.com/raffaelelucifer/vue_cmdb 老师您好,这是github地址。 具体就是 “自动化平台>平台1-发布代码”那个页面,无法实现表单联动,“请选择项目”那一栏里项目的选项不能根据产品线的选择来改变,这个组件代码的目录为:https://github.com/raffaelelucifer/vue_cmdb/tree/master/cmdb/src/components/auto_codes 非常感谢老师的帮助!
2019-02-18
共1条回复

Vue.js2.5+cube-ui重构饿了么App(经典再升级)

掌握Vue1.0到2.0再到2.5最全版本应用与迭代,打造极致流畅的WebApp

9868 学习 · 4162 问题

查看课程