vue js readdir
<template>
<div>
<ul>
<li v-for="file in files" :key="file">{{ file }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
files: [],
};
},
mounted() {
this.readDirectory();
},
methods: {
async readDirectory() {
try {
const response = await this.$axios.get('/api/read-directory');
this.files = response.data;
} catch (error) {
console.error('Error reading directory:', error);
}
},
},
};
</script>
In this example:
The template section defines a simple Vue component with a list (
ul
) to display files.The
data
function initializes a data propertyfiles
to store the list of files.The
mounted
lifecycle hook calls thereadDirectory
method when the component is mounted.The
readDirectory
method is an asynchronous function that uses Axios (assumed as a plugin) to make a GET request to the '/api/read-directory' endpoint.Upon a successful response, the
files
data property is updated with the list of files obtained from the server.If there is an error during the GET request, an error message is logged to the console.
Note: Make sure to replace '/api/read-directory' with the actual endpoint that provides the list of files in your application.