我的vue组件如下:
<template>
...
<a href="javascript:;" class="thumbs"
:title="upload">
<span class="fa fa-plus fa-2x"></span>
</a>
...
</template>
<script>
export default {
props: ['...'],
data() {
return {
...
};
},
computed:{
...
}
}
</script>我想如果点击一个链接,它可以上传文件
在javascript中,我知道它。如果javascript像这样:How to make a link act as a file input
但是如何在vue.js 2中做到这一点呢?
发布于 2017-06-23 02:11:22
我相信有一个小小的误解: Vue.js 2仍然是javascript。它的目标与具有奇特components的聚合物不同-它应该增强JS,而不是完全用不同的结构取代它。
@David Hallberg Jönsson的答案在Vue.js 2中运行得非常好,非常好。如果您特别想在Vue的组件结构中使用它:
<template>
<!-- ... -->
<a class="fileContainer">
Click here to trigger the file uploader!
<input type="file">
</a>
<!-- ... -->
</template>
<script>
export default {
props: ['...'],
data() {
return {
...
};
},
computed:{
...
}
}
</script>
<style>
a.fileContainer {
overflow: hidden;
position: relative;
cursor: pointer;
display: inline-block;
color: lightskyblue;
}
a.fileContainer:hover {
text-decoration: underline;
color: blue;
}
a.fileContainer > input[type=file] {
cursor: inherit;
filter: alpha(opacity=0);
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: right;
}
</style>如果你想在你的链接中使用编程的方式,这并不容易,因为some browsers don't allow you to trigger click events on input type="file" elements。你最好走这条路。
(此外,从技术上讲,您仍然可以将jQuery与Vue一起使用,因此,如果您愿意,该链接中的代码仍然可以工作。)
如果你想知道如何处理上传文件,有已经预先准备好的many tutorials和some components。
发布于 2017-06-22 20:59:52
你只需要使用CSS,as explained here就可以做到这点。
示例(来自上面的链接):
.fileContainer {
overflow: hidden;
position: relative;
}
.fileContainer [type=file] {
cursor: inherit;
display: block;
font-size: 999px;
filter: alpha(opacity=0);
min-height: 100%;
min-width: 100%;
opacity: 0;
position: absolute;
right: 0;
text-align: right;
top: 0;
}
/* Example stylistic flourishes */
.fileContainer {
background: red;
border-radius: .5em;
float: left;
padding: .5em;
}
.fileContainer [type=file] {
cursor: pointer;
}
}<p>So various methods prevent file upload inputs from being styled conveniently. But that needn't be the case!</p>
<label class="fileContainer">
Click here to trigger the file uploader!
<input type="file"/>
</label>
https://stackoverflow.com/questions/44699986
复制相似问题