<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue-upload-component Test</title> <script src="https://unpkg.com/vue"></script> <script src="https://unpkg.com/vue-upload-component"></script> </head> <body> <div id="app"> <ul> <li v-for="file in files">{{file.name}} - Error: {{file.error}}, Success: {{file.success}}</li> </ul> <file-upload ref="upload" v-model="files" post-action="/post.method" put-action="/put.method" @input-file="inputFile" @input-filter="inputFilter" > Upload file </file-upload> <button v-show="!$refs.upload || !$refs.upload.active" @click.prevent="$refs.upload.active = true" type="button">Start upload</button> <button v-show="$refs.upload && $refs.upload.active" @click.prevent="$refs.upload.active = false" type="button">Stop upload</button> </div> <script> new Vue({ el: '#app', data: function () { return { files: [] } }, components: { FileUpload: VueUploadComponent }, methods: { /** * Has changed * @param Object|undefined newFile Read only * @param Object|undefined oldFile Read only * @return undefined */ inputFile: function (newFile, oldFile) { if (newFile && oldFile && !newFile.active && oldFile.active) { // Get response data console.log('response', newFile.response) if (newFile.xhr) { // Get the response status code console.log('status', newFile.xhr.status) } } }, /** * Pretreatment * @param Object|undefined newFile Read and write * @param Object|undefined oldFile Read only * @param Function prevent Prevent changing * @return undefined */ inputFilter: function (newFile, oldFile, prevent) { if (newFile && !oldFile) { // Filter non-image file if (!/\.(jpeg|jpe|jpg|gif|png|webp)$/i.test(newFile.name)) { return prevent() } } // Create a blob field newFile.blob = '' let URL = window.URL || window.webkitURL if (URL && URL.createObjectURL) { newFile.blob = URL.createObjectURL(newFile.file) } } } }); </script> </body> </html> 将这个页面改为 vue3 的写法
时间: 2024-03-27 17:35:34 浏览: 86
HTML中的<meta>标签的使用详解
5星 · 资源好评率100%
好的,下面是将这个页面改为 Vue 3 的写法:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue-upload-component Test</title>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/vue-upload-component@3.0.0-beta.6"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="file in files">{{file.name}} - Error: {{file.error}}, Success: {{file.success}}</li>
</ul>
<file-upload
ref="upload"
v-model="files"
:post-action="'/post.method'"
:put-action="'/put.method'"
@input-file="inputFile"
@input-filter="inputFilter"
>
Upload file
</file-upload>
<button v-show="!$refs.upload || !$refs.upload.active" @click.prevent="$refs.upload.active = true" type="button">Start upload</button>
<button v-show="$refs.upload && $refs.upload.active" @click.prevent="$refs.upload.active = false" type="button">Stop upload</button>
</div>
<script>
const { createApp, ref } = Vue;
const { VueUploadComponent } = vueUploadComponent;
createApp({
setup() {
const files = ref([]);
const inputFile = (newFile, oldFile) => {
if (newFile && oldFile && !newFile.active && oldFile.active) {
// Get response data
console.log('response', newFile.response);
if (newFile.xhr) {
// Get the response status code
console.log('status', newFile.xhr.status);
}
}
};
const inputFilter = (newFile, oldFile, prevent) => {
if (newFile && !oldFile) {
// Filter non-image file
if (!/\.(jpeg|jpe|jpg|gif|png|webp)$/i.test(newFile.name)) {
return prevent();
}
}
// Create a blob field
newFile.blob = ''
let URL = window.URL || window.webkitURL
if (URL && URL.createObjectURL) {
newFile.blob = URL.createObjectURL(newFile.file)
}
};
return {
files,
inputFile,
inputFilter
}
},
components: {
'file-upload': VueUploadComponent
}
}).mount('#app');
</script>
</body>
</html>
```
主要变化如下:
- 使用 `createApp` 创建 Vue 实例;
- 使用 `ref` 创建响应式变量;
- 使用 `setup` 函数代替 `data` 属性;
- 使用 `VueUploadComponent` 替代 `vueUploadComponent`;
- 使用 `v-bind` 缩写 `:` 替代 `v-bind`;
- 使用 `v-on` 缩写 `@` 替代 `v-on`;
- 使用 `file-upload` 替代 `file-upload`。
阅读全文