[英]how can i add style to vue component
我有這個代碼。 它是一個 base64 文件輸入的組件。 它還支持拖放。 我需要拖放樣式。 但它每次都會刪除樣式。 渲染后,樣式標簽消失,樣式未應用。 如何為這個組件添加樣式?
謝謝你。
Vue.component('file-base64', {
template:`
<div class="custom-file" style="padding:0;" @dragenter="ondragenter" @dragover="" @dragleave="ondragleave" @drop="ondrop">
<input :id="inputid" class="custom-file-input" type="file" @change="onChange" :multiple="multiple" />
<label :for="inputid" class="custom-file-label">{{label}}</label>
<style>
#{{inputid}} .fileuploaddrophover {height:auto; border:2px dotted gold;}
#{{inputid}} .fileuploaddrophover > label {opacity: 0.5; filter:blue(5px);}
</style>
</div>
`,
data: function () {
return {inputid:(function(){var x; do{x= "filebase64input"+Math.round(Math.random()*100000);} while(document.getElementById(x)) return x;})()};
},
props:{
label:{type:String,default:"Drop or Choose file..."},
multiple: {type: Boolean,default: false},
done: {type: Function,default: () => {}}
},
methods: {
onChange(e){
let files = e.target.files;
var allFiles = [];
for (var i = 0; i < files.length; i++){
let file = files[i];
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
let fileInfo = {name: file.name,type: file.type,size: file.size,base64: reader.result.split(',')[1],file: file};
allFiles.push(fileInfo);
if(allFiles.length == files.length){
if(this.multiple) this.done(allFiles);
else this.done(allFiles[0]);
}
}
}
},
ondragenter : function (e) {
$("#"+this.inputid).parent().addClass('fileuploaddrophover');
},
ondragleave : function (e) {
$("#"+this.inputid).parent().removeClass('fileuploaddrophover');
},
ondrop : function (e) {
$("#"+this.inputid).parent().removeClass('fileuploaddrophover');
},
},
});
我終於找到了辦法。
由於數據是函數,因此每次添加元素時都會調用它。
data: function () {
return {
inputid:(function(){var x; do{x= "filebase64input"+Math.round(Math.random()*100000);} while(document.getElementById(x)) return x;})(),
cssrules:(function(){
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = `
.fileuploaddrophover { height:auto; border:2px dotted gold; }
.fileuploaddrophover > label {opacity: 0.5; filter:blue(5px); }
`;
document.body.appendChild(css);
})(),
};
},
這絕對是一個黑客,但它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.