javascript - CropperJs 图像在 Vue 中很小,但在编辑后恢复正常大小
问题描述
这是一个非常奇怪的行为,我不知道该怎么做,我正在建立一个使用 Vue.js 和 CropperJs 裁剪图像的网站,在主页上用户选择他们想要裁剪的图像并点击“继续” ,下一页显示了一个名为 的组件ImageCropper.vue
,画布和所有内容都已成功显示,但它很小,但是假设我编辑了 html,即使我只添加了一个空行,图像也会恢复到应有的大小(我没有我想它没有预定义的大小,所以它只需要容器宽度的 100% 并且高度是自动的。)
ImageCropper.vue
<template>
<div class="image-container">
<img ref="image" :src="source">
</div>
</template>
<script>
import Cropper from 'cropperjs'
export default {
name: "ImageCropper",
props: ["source"],
data() {
return {
cropper: null,
imageElement: null
}
},
mounted() {
this.imageElement = this.$refs.image
this.cropper = new Cropper(this.imageElement, {
aspectRatio: 1
})
}
}
</script>
我尝试用css对其进行样式设置,但似乎没有任何效果,也许我使用vue的方式有问题?如果重要的话,这是带有 Vuetify 的 Vue。
解决方案
有一个可用的包装器,它的 vue-cropperjs,检查一下。
<template>
<div>
<vue-cropper ref="cropper" :src="source" alt="Source Image"></vue-cropper>
<v-btn @click="save">Save</v-btn>
</div>
</template>
<script>
import VueCropper from 'vue-cropperjs';
import 'cropperjs/dist/cropper.css';
export default{
name: "ImageCropper",
components: {
VueCropper
},
methods: {
save: function(){
this.$refs.cropper.getCroppedCanvas().toBlob((blob) => {
//Do anything with the blob, for eg: attach to form data and send to serve
const formData = new FormData();
formData.append("type", "profile");
formData.append('file', blob, this.fileName);
});
}
}
}
</script>
推荐阅读
- testng - 无法从类中获取 TestNG 版本:java.lang.ClassNotFoundException:org.testng.internal.Version
- google-apps-script - 将多个工作表导出到多个 PDF 并添加到 Sendmail 作为 HTML 正文的附件
- scala - 如何在 Scala 中创建模拟对象?
- php - 在 laravel 应用程序中实现适配器设计模式
- android - 在 Android 中为 StaggeredGridlayoutManager 的间隙填充颜色
- python-3.x - 有没有办法在 tkinter 的 for 循环中创建一个 get 方法
- php - 从 cookie 设置会话变量是个好主意吗?
- docker - 我在哪里可以找到这个 golang 图像的 windows 版本的名称?
- javascript - 如何将尾随元数据从 python gRPC 服务发送到 grpc-web 客户端?
- c - 在C中将文本文件的特定部分作为字符串读取?