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

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

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

關(guān)鍵點: 父組件給子組件動態(tài)傳參使用v-bind:屬性key(多個單詞用下劃線拼接) 子組件接收父組件傳參參數(shù)使用 props標簽,+屬性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)文章

最新評論

上林县| 吉首市| 庆元县| 青神县| 桃江县| 安福县| 桐梓县| 南郑县| 米泉市| 阳城县| 阿克| 天津市| 且末县| 贡嘎县| 古交市| 阳原县| 麻江县| 普宁市| 南昌市| 嘉义市| 昌宁县| 桦南县| 油尖旺区| 桃园县| 开远市| 博野县| 长顺县| 体育| 理塘县| 门源| 龙门县| 瑞安市| 蒲城县| 常熟市| 金堂县| 新蔡县| 皋兰县| 宜丰县| 明光市| 永顺县| 威宁|