vue编辑页面如何接收数据?

我用的是created()方法。里面调用按照id查询数据的方法。但是我在页面的提示信息,无论怎样调用别的方法。那个查询数据的方法,里面的提示信息一直都处于最高优先级。即使我手动去修改了提示信息。在控制台打印出来也是修改过的。但是页面的提示信息就是毫无反应。不知道是为什么?

image.png
`<template>
<div class="editeRepo">
<el-form :model="ruleForm" status-icon
:rules="rules"
:with-credentials='true'
ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="试卷名称:" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item> <el-form-item label="试卷描述:" prop="description">
<el-input v-model="ruleForm.description" type="textarea" :rows="2"></el-input>
</el-form-item> <el-form-item label="试卷标签:" prop="categories">
<el-tag :key="index"
v-for="(tag,index) in ruleForm.categories"
closable
:disable-transitions="false"
@close="handleClose(tag)">
{{tag}}

            </el-tag>  

<el-input class="input-new-tag"
v-model="ruleForm.inputValue"
ref="saveTagInput"
size="small"
placeholder="请输入"
@keyup.enter.native="handleInputConfirm"
@blur="handleInputConfirm"
>
</el-input> </el-form-item>
<el-form-item label="是否私有:">
<el-form-item> <el-radio v-model="ruleForm.personal" :label="false">公开</el-radio>
<el-radio v-model="ruleForm.personal" :label="true">私有</el-radio>
</el-form-item> </el-form-item> <el-form-item type="text" prop="tip">{{ruleForm.tip}}</el-form-item>
<el-form-item> <el-button type="primary" @click="updateExamRepo('ruleForm')">保存</el-button>
</el-form-item> </el-form> </div></template>

<script>
import global from "../../Global";

export default {

    name: "EditExam",  

data() {

        var valiName \= (rule, value, callback) => {  
            value = value.trim();  

console.log("value============" + value);
console.log(!value);
if (value) {

                if (value === this.oldName) {  
                    console.log("this.ruleForm.name==="+this.oldName);  

console.log('试卷名称与之前的名称相同');
callback();

// return callback(new Error('试卷名称与之前的名称相同'));
}else if (this.isExamNameTaken(value) === true) {

                    console.log('试卷名称已存在');  

return callback(new Error('试卷名称已存在'));
} else {

                    callback();  

}

            } else {  
                return callback(new Error('请输入试卷名称'));  

}

        };  

return {

            examId: '',  

oldName: '',
ruleForm: {

                name: '',  

description: '',
classification: '',
categories: [],
inputValue: '',
personal: false,//是否私有,默认false是公开
tip: '温馨提示',
},
rules: {

                name: \[  
                    {validator: valiName,trigger: 'blur'},  

{min: 2, max: 50, message: '长度在 2 到 50 个字符'}

                \],  

description: [

                    {message: '请输入试卷描述', trigger: 'blur'},  

{min: 2, max: 50, message: '长度在 2 到 50 个字符'}

                \],  

personal: [

                    {message: '请选择试卷是否私有', trigger: 'change'},  

],
},
}

    },  

methods: {

        // 更新试卷信息  

updateExamRepo(formName) {

            console.log(formName);  

this.ruleForm.tip="这是一个tip";
// this.$refs[formName].validate((valid) => {
// if (valid) { // let formdata = new FormData(); // formdata.append('id', this.ruleForm.id); // formdata.append('name', this.ruleForm.name); // formdata.append('description', this.ruleForm.description); // formdata.append('categories', JSON.stringify(this.ruleForm.categories)); // // formdata.append('categories', this.ruleForm.categories); // formdata.append('personal', this.ruleForm.personal); // this.$axios.post("/exam/updateExam", formdata).then(response => { // // console.log(response.data); // if (response.data.status === global.responseCode.ACCESS_DENIED) { // console.log("目前未登录!请先登录!");
// this.ruleForm.tip = "目前未登录!请先登录!2秒后会自动跳转到登录页面~~~";
// // 直接调用$router.push 实现路由的的跳转,2s后跳转
// setInterval(() => {
// this.$router.push({path: '/login'}); // }, 2000); // } else if (response.data.status === global.responseCode.OK) { // console.log("试卷更新成功");
// this.ruleForm.tip = "试卷更新成功!";
// console.log("Tip: "+ this.ruleForm.tip); // // this.findRepoById(this.examId); // } else { // this.ruleForm.tip = "试卷更新失败: " + response.data.msg;
// console.log("试卷更新失败:" + response.data.msg);
// } // }).catch(function (error) { // console.log("*************"); // console.log(error); // }); // // } else { // this.ruleForm.tip = '请补充完整!!';
// console.log('请补充完整!!');
// return false; // } // });

},
// 按照试卷编号查找试卷
findRepoById(examId) {

            this.$axios.get("/exam/findExamById", {params: {id: examId}}).then(response => {  
                console.log(response.data.result);  

console.log(response);
if (response.data.status === global.responseCode.OK) {

                    this.ruleForm \= response.data.result;  

this.oldName = response.data.result.name;
console.log("response.data.result.categories=====" + response.data.result.categories);
console.log("试卷信息获取成功");

this.ruleForm.tip = "试卷信息获取成功,修改后请记得点击保存哦!~~~";
} else {

                    this.ruleForm.tip \= "试卷创建失败: " \+ response.data.msg;  

console.log("试卷创建失败:" + response.data.msg);
}

            }).catch(function (error) {  
                console.log("\*\*\*\*\*\*\*\*\*\*\*\*\*");  

console.log(error);
});

},
// 查询试卷名是否被占用
isExamNameTaken(name) {

            this.$axios.get("/exam/isExamNameTaken", {params: {name: name, examId: this.examId}}).then(response => {  
                console.log("名称是否被占用???" \+ response.data.result);  

console.log(response);
if (response.data.status === global.responseCode.OK) {

                    console.log("试卷名称验证信息获取成功");  

console.log("试卷名称验证创建失败:" + response.data.msg);

// this.ruleForm.tip = "试卷名称验证信息获取成功,修改后请记得点击保存哦!~~~";
} else {

                    // this.ruleForm.tip = "试卷名称验证创建失败: " + response.data.msg;  

console.log("试卷名称验证创建失败:" + response.data.msg);
}

            }).catch(function (error) {  

                console.log("\*\*\*\*\*\*\*\*\*\*\*\*\*");  

console.log(error);
});
},
// 删除标签名
handleClose(tag) {

            this.ruleForm.categories.splice(this.ruleForm.categories.indexOf(tag), 1);  

console.log(tag);
},
//输入标签名
handleInputConfirm() {

            let inputValue = this.ruleForm.inputValue;  

if (!inputValue || inputValue === "" || inputValue === null) {

                console.log("标签名不能为空!");  

} else {

                this.ruleForm.categories.push(inputValue);  

}

            console.log(inputValue);  

console.log(this.ruleForm.categories);

this.ruleForm.inputValue = '';
},
},
created() {

        console.log(localStorage.getItem("examId"));  

this.examId = localStorage.getItem("examId");
console.log(this.examId);
this.findRepoById(this.examId);
console.log(this.$refs);

}

}  

</script>

<style scoped>
.el-tag + .el-tag {

    margin-left: 10px;  

}

.button-new-tag {  
    margin-left: 10px;  

height: 32px;
line-height: 30px;
padding-top: 0;
padding-bottom: 0;
}

.input-new-tag {  
    width: 90px;  

margin-left: 10px;
vertical-align: bottom;
}
</style>`

阅读 3.9k
4 个回答

贴的比较乱,你可以使用三点的形式来展示你的代码块,并且删除一些无关的业务代码。
现在看你的代码会很累。
Snipaste_2020-03-06_13-25-33.jpg

我觉得是可能你修改的提示文本是对象内的属性,
可能不会被侦测到改变,你尝试下把提示文本 tip 提到外部试试。


我给你整理了一下,去除了样式代码和你注释掉的快,其它想答题的可以考虑看我这边的业务代码

<template>
  <div class="editeRepo">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      :with-credentials="true"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="试卷名称:" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="试卷描述:" prop="description">
        <el-input
          v-model="ruleForm.description"
          type="textarea"
          :rows="2"
        ></el-input>
      </el-form-item>
      <el-form-item label="试卷标签:" prop="categories">
        <el-tag
          :key="index"
          v-for="(tag, index) in ruleForm.categories"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-model="ruleForm.inputValue"
          ref="saveTagInput"
          size="small"
          placeholder="请输入"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="是否私有:">
        <el-form-item>
          <el-radio v-model="ruleForm.personal" :label="false">公开</el-radio>
          <el-radio v-model="ruleForm.personal" :label="true">私有</el-radio>
        </el-form-item>
      </el-form-item>
      <el-form-item type="text" prop="tip">{{ ruleForm.tip }}</el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateExamRepo('ruleForm')"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import global from "../../Global";
export default {
  name: "EditExam",
  data() {
    var valiName \= (rule, value, callback) => {
      value = value.trim();
      console.log("value============" + value);
      console.log(!value);
      if (value) {
        if (value === this.oldName) {
          console.log("this.ruleForm.name==="+this.oldName);
          console.log('试卷名称与之前的名称相同');
          callback();
          // return callback(new Error('试卷名称与之前的名称相同'));
        }else if (this.isExamNameTaken(value) === true) {
          console.log('试卷名称已存在');
          return callback(new Error('试卷名称已存在'));
        } else {
          callback();
        }
      } else {
        return callback(new Error('请输入试卷名称'));
      }
    };
    return {
      examId: '',
      oldName: '',
      ruleForm: {
        name: '',
        description: '',
        classification: '',
        categories: [],
        inputValue: '',
        personal: false,//是否私有,默认false是公开
        tip: '温馨提示',
      },
      rules: {
        name: [{validator: valiName,trigger: 'blur'},{min: 2, max: 50, message: '长度在 2 到 50 个字符'}],
        description: [{message: '请输入试卷描述', trigger: 'blur'},{min: 2, max: 50, message: '长度在 2 到 50 个字符'}],
        personal: [{message: '请选择试卷是否私有', trigger: 'change'},],
      },
    }
  },
  methods: {
    // 更新试卷信息
    updateExamRepo(formName) {
      console.log(formName);
      this.ruleForm.tip="这是一个tip";
    },
    // 按照试卷编号查找试卷
    findRepoById(examId) {
      this.$axios.get("/exam/findExamById", {params: {id: examId}}).then(response => {
        console.log(response.data.result);
        console.log(response);
        if (response.data.status === global.responseCode.OK) {
          this.ruleForm = response.data.result;
          this.oldName = response.data.result.name;
          console.log("response.data.result.categories=====" + response.data.result.categories);
          console.log("试卷信息获取成功");
          this.ruleForm.tip = "试卷信息获取成功,修改后请记得点击保存哦!~~~";
        } else {
          this.ruleForm.tip = "试卷创建失败: " + response.data.msg;
          console.log("试卷创建失败:" + response.data.msg);
        }
      }).catch(function (error) {
        console.log("\*\*\*\*\*\*\*\*\*\*\*\*\*");
        console.log(error);
      });
    },
    // 查询试卷名是否被占用
    isExamNameTaken(name) {
      this.$axios.get("/exam/isExamNameTaken", {params: {name: name, examId: this.examId}}).then(response => {
        console.log("名称是否被占用???" + response.data.result);
        console.log(response);
        if (response.data.status === global.responseCode.OK) {
          console.log("试卷名称验证信息获取成功");
          console.log("试卷名称验证创建失败:" + response.data.msg);
          // this.ruleForm.tip = "试卷名称验证信息获取成功,修改后请记得点击保存哦!~~~";
        } else {
          // this.ruleForm.tip = "试卷名称验证创建失败: " + response.data.msg;
          console.log("试卷名称验证创建失败:" + response.data.msg);
        }
      }).catch(function (error) {
        console.log("\*\*\*\*\*\*\*\*\*\*\*\*\*");
        console.log(error);
      });
    },
    // 删除标签名
    handleClose(tag) {
      this.ruleForm.categories.splice(this.ruleForm.categories.indexOf(tag), 1);
      console.log(tag);
    },
    //输入标签名
    handleInputConfirm() {
      let inputValue = this.ruleForm.inputValue;
      if (!inputValue || inputValue === "" || inputValue === null) {
        console.log("标签名不能为空!");
      } else {
        this.ruleForm.categories.push(inputValue);
      }
      console.log(inputValue);
      console.log(this.ruleForm.categories);
      this.ruleForm.inputValue = '';
    },
  },
  created() {
    console.log(localStorage.getItem("examId"));
    this.examId = localStorage.getItem("examId");
    console.log(this.examId);
    this.findRepoById(this.examId);
    console.log(this.$refs);
  }
}
</script>

在程序里修改并不会触发表单验证,需要手动触发。
在给ruleForm赋值后,使用this.$refs.ruleForm.validate()手动触发验证就行了。

帖点代码看看啊
你有没有把数据写入到data中啊

感谢 @陟上晴明 老哥把代码整理出来了...
说一下问题吧

题主希望修改 ruleForm.tip 的值,但是修改不成功
截图

如果我没找错的话~

然后我没找到后续修改,我猜测是下面那部分方法
如果有找错的话请提供一下方法名~

然后在看到绑定 ruleForm.tip 的控件

<el-form-item type="text" prop="tip">{{ ruleForm.tip }}</el-form-item>

没用过element-ui不清楚内部怎封装的,理论上 ruleForm.tip 值修改的时候视图是会相应绑定,我个人建议吧用一个普通的div绑定修改的变量,看看能否出现改变~

<div>{{ ruleForm.tip }}</div>

推荐问题