最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用

 更新時間:2023年05月11日 09:18:03   作者:gblfy  
這篇文章主要介紹了關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用,我們將某段代碼封裝成一個組件,而這個組件又在另一個組件中引入,而引入該封裝的組件的文件叫做父組件,被引入的組件叫做子組件,需要的朋友可以參考下

關(guān)鍵點(diǎn): 父組件給子組件動態(tài)傳參使用v-bind:屬性key(多個單詞用下劃線拼接) 子組件接收父組件傳參參數(shù)使用 props標(biāo)簽,+屬性key多個單詞用駝峰形式拼接)

子組件定義回調(diào)父組件函數(shù) 子組件: v-on:change="uploadFile()

父組件: :after-upload=“afterUpload” 然后定一個afterUpload(resp)方法接收子組件傳過來的值

    <div class="col-sm-10">
                  <file :text="'上傳頭像1'"
                        :input-id="'image-upload'"
                        :suffixs="[ ['jpg', 'jpeg', 'png']]"
                        :after-upload="afterUpload">
                  </file>
<script>
import File from "../../components/file";
export default {
  components: {File},
  name: "business-teacher",
  data: function () {
  },
  mounted: function () {
  },
  methods: {
    afterUpload(resp) {
      let _this = this
      let image = resp.content;
      _this.teacher.image = image
    }
  }
}
</script>

子組件

<template>
  <div>
    <button type="button" v-on:click="selectFile()" class="btn btn-white btn-default btn-round">
      <i class="ace-icon fa fa-upload"></i>
      {{ text }}
    </button>
    <input class="hidden" type="file"
           ref="file"
           v-on:change="uploadFile()"
           v-bind:id="inputId+'-input'">
  </div>
</template>
<script>
export default {
  name: 'file',
  props: {
    text: {
      default: "上傳文件"
    },
    inputId: {
      default: "file-upload"
    },
    suffixs: {
      default: []
    },
    afterUpload: {
      type: Function,
      default: null
    },
  },
  data: function () {
    return {}
  },
  methods: {
    uploadFile() {
      let _this = this;
      let formData = new window.FormData();
      let file = _this.$refs.file.files[0];
      // 判斷文件格式
      let suffixs = _this.suffixs;
      let fileName = file.name;
      let suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
      let validateSuffix = false;
      for (let i = 0; i < suffixs.length; i++) {
        let suffix2 = suffixs[i] += ''
        if (suffix2.toLowerCase() === suffix) {
          validateSuffix = true;
          break;
        }
      }
      if (!validateSuffix) {
        Toast.warning("文件格式不正確,只支持上傳:" + suffixs.join(","));
        //解決 同一個文件上傳2次或者大于2次,不會發(fā)生變化
        $("#" + _this.inputId + "-input").val("");
        return
      }
      // key:"file"必須和后端controller參數(shù)名一致
      formData.append("file", file);
      Loading.show()
      _this.$api.post(process.env.VUE_APP_SERVER + '/file/admin/upload', formData).then((response) => {
        Loading.hide()
        let resp = response.data
        console.log("上傳文件成功:", resp)
        //回調(diào)父組件函數(shù)
        _this.afterUpload(resp)
        //解決 同一個文件上傳2次或者大于3次,不會發(fā)生變化
        $("#" + _this.inputId + "-input").val("");
      })
    },
    selectFile() {
      let _this = this
      // console.log("_this.inputId",_this.inputId)
      $("#" + _this.inputId + "-input").trigger("click");
    }
  },
}
</script>
<style scoped>
</style>

到此這篇關(guān)于關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用的文章就介紹到這了,更多相關(guān)Vue父子組件回調(diào)函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

武乡县| 武汉市| 陆丰市| 乐平市| 临泉县| 湘潭市| 西充县| 会泽县| 古蔺县| 台山市| 桂阳县| 临沂市| 当涂县| 恩施市| 汉寿县| 灌云县| 兴隆县| 琼结县| 潞西市| 宁明县| 绍兴县| 永定县| 灌云县| 沙河市| 永胜县| 钟祥市| 瓦房店市| 岫岩| 鹤庆县| 景德镇市| 庆云县| 巴里| 潮州市| 金湖县| 万宁市| 辽源市| 江津市| 佛教| 宜宾市| 泽库县| 纳雍县|