当前位置: 首页 > 知识库问答 >
问题:

javascript - vue2弹窗打开没有加载状态?

饶滨海
2023-11-03
<template>  <div id="app">    <el-dialog :visible.sync="isVisible">      <template slot="title">        <div>          <span>图片查看</span>        </div>      </template>      <div v-loading="imgLoading">        <img          :src="currentImage"          v-show="imgDataArr.length > 0 && !imgLoading"          @load="imageLoaded"          @error="imageLoadedFailed"          alt=""        >      </div>    </el-dialog>  </div></template><script>export default {  data() {    return  {      isVisible: this.fileImageData.show,      currentIndex: '',      imgDataArr: [],      currentImage: '',      imgLoading: true    }  },  props: {    fileImageData: Object  },  watch:{    currentIndex: {      hanler(newV){        if(this.imgDataArr.length > 0) {          const formData = new FormData()          formData.append('file', this.imgDataArr[newV].attId)          const res = downloadFileImage(formData)          res.then(blob => {            if(blob instanceof Blob) {              const url = window.URL.createObjectURL(blob)              this.currentImage = url            }          })        }      }    }  },  mounted() {    this.getImage()  },  methods: {    getImage() {      const arr = []      this.fileImageData.imageData.map(item => {        arr.push({attId:item.attId})      })      this.imgDataArr = arr      this.currentIndex = 0    },    imageLoaded() {      this.imgLoading = false    },    imageLoadedFailed() {      this.imgLoading = false    }  }}</script>

这是子组件,上面这样写,弹窗打开的时候没有加载状态,是什么原因

共有2个答案

郑胡媚
2023-11-03
<template>  <!-- ... -->  <div v-loading="imgLoading">    <img      :src="currentImage"      v-if="currentImage" <!-- 用 v-if 替换 v-show ->      @load="imageLoaded"      @error="imageLoadedFailed"      alt=""    >  </div>  <!-- ... --></template><script>export default {  // ...  watch:{    // 修改 typo    currentIndex: {      handler(newV, oldV) {        if(newV !== oldV) {          this.imgLoading = true;           // ... your code to load the image        }      },      immediate: true // watcher绑定后立马触发    }  },  methods: {    // ...    getImage() {      // ... your code      this.imgLoading = true;     },    imageLoaded() {      this.imgLoading = false;    },    imageLoadedFailed() {      this.imgLoading = false;      // 错误处理    }  }}</script>
司空高义
2023-11-03

你的代码中使用了 v-loading 指令来控制图片加载的状态,然而这个指令只有在 Vue 2.x 的版本中才有。如果你在 Vue 3.x 中使用这段代码,就会发现 v-loading 指令不再可用。

在 Vue 3.x 中,官方已经弃用了 v-loading 指令,并推荐使用第三方库,例如 vue-loading-overlay

如果你仍然想在 Vue 2.x 中使用这段代码,你需要确保你已经正确安装并引入了 vue-loading-overlay。你可以通过以下命令来安装:

npm install vue-loading-overlay --save

然后在你的组件中引入并使用:

<template>  <div id="app">    <el-dialog :visible.sync="isVisible">      <template slot="title">        <div>          <span>图片查看</span>        </div>      </template>      <div>        <vue-loading-overlay :active.sync="imgLoading" text="正在加载...">          <img            :src="currentImage"            v-show="imgDataArr.length > 0 && !imgLoading"            @load="imageLoaded"            @error="imageLoadedFailed"            alt=""          >        </vue-loading-overlay>      </div>    </el-dialog>  </div></template>

注意,在 Vue 3.x 中,你可能需要调整你的代码以适应新的生命周期方法和新的语法特性。例如,你可以使用 setup 方法来引入和使用 vue-loading-overlay

<template>  <div id="app">    <el-dialog :visible.sync="isVisible">      <template slot="title">        <div>          <span>图片查看</span>        </div>      </template>      <div>        <vue-loading-overlay :active.sync="imgLoading" text="正在加载...">          <img            :src="currentImage"            :hidden="imgDataArr.length <= 0 || imgLoading"            @load="imageLoaded"            @error="imageLoadedFailed"            alt=""          >        </vue-loading-overlay>      </div>    </el-dialog>  </div></template>
 类似资料:
  • 问题内容: 我正在尝试用Java(仅我知道的语言,我刚刚学习线程)创建一个自动单击器。我希望在其自己的窗口(而不是在网页上)中打开小程序,并且希望能够在不选择窗口的情况下使用空格键启动和停止该程序,以便可以在其他程序上使用自动单击器并能够停止它而无需alt-f4一堆东西。 您有什么可以推荐我的,可以帮助我实现这一目标的吗?或您有什么建议吗? 问题答案: 这可能超出了Java小程序的范围。实际上,全

  • 我有一个下载操作在我的javascript代码与提供商。 这将触发我的浏览器弹出窗口。我不想这样。 但是我想直接下载。不要显示弹出窗口。

  • 问题内容: 我正在尝试使用javafx在webview中打开网页。单击超链接后,此网页将打开一个新的弹出窗口。 我如何打开新的弹出窗口,当尝试在默认Web浏览器(例如chrome,IE)中打开同一网页时,它们正在打开弹出窗口。 为了创建弹出窗口,我使用以下代码。 问题答案: 您需要自己创建WebView弹出窗口,并从回调中提供WebEngine。如果需要新窗口,请使用该WebView创建一个新的舞

  • 问题内容: 当我在Selenium中运行测试时,新打开的firefox窗口将打开,而没有安装我的插件(如xpathchecker)。 是否可以设置selenium,以便它可以将Firefox与已安装的插件一起使用? 问题答案: 如果您使用硒遥控器, 您可能需要创建一个单独的配置文件以使用附加组件进行测试。 之后,您可以在该新配置文件中安装插件。在配置文件管理器中记下配置文件文件夹的路径,或在此处查

  • 问题内容: 我在JPopupMenu中有一个JComboBox(以及其他组件)。事实证明,每当我打开组合框的弹出窗口(以选择一个项目)时,父级JPopupMenu都会关闭。我一直在尝试找到一种方法来覆盖此功能,但无济于事。 有没有人建议防止关闭父级JPopupMenu?谢谢! 问题答案: 这不可能直接实现,很难覆盖已知的错误,另一方面,Swing不允许同时使用两个lightwieght弹出组件 但