如何存储和保存数据
我开始学习商店,我想上传图像,并将其保存在本地上。因此,如果我重新加载页面映像必须保存
存储photoupload.js
const state = {
photo: '1'
}
const getters = {
getPhoto: (state) => {
return state.photo
}
}
const mutations = {
setPhoto(state, photoName) {
state.photo = photoName
},
}
const actions = {
getPhoto(context, photo) {
context.commit('getPhoto', photo)
}
}
export const testings = {
namespaced: true,
state,
mutations,
getters,
actions,
};
模板
<div class="upload">
<img :src="previewImage ? previewImage : 'assets/images/defaultAva.png'" class="profileImg" />
<label class="edit">
<input ref="imageInput" type="file" name="file" accept="image/png, image/jpeg, image/jpg" @change="uploadImage">
</label>
</div>
<script>
data: () => ({
previewImage: null,
}),
computed: {
getPhoto() {
return this.$store.getters["photoUpload/getPhoto"];
},
},
methods: {
uploadImage(e){
const image = e.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(image);
reader.onload = e =>{
this.previewImage = e.target.result;
};
},
...mapActions("testings", ["getPhoto"]),
},
</script>
,那么我必须以巫婆的方式去,我可以上传图像以预览图像,但是如何将其发送到存储,以在本地保存它?
I start to learning store, and i want to upload image, and save it on local. So if i reloaded page image must be saved
store photoUpload.js
const state = {
photo: '1'
}
const getters = {
getPhoto: (state) => {
return state.photo
}
}
const mutations = {
setPhoto(state, photoName) {
state.photo = photoName
},
}
const actions = {
getPhoto(context, photo) {
context.commit('getPhoto', photo)
}
}
export const testings = {
namespaced: true,
state,
mutations,
getters,
actions,
};
template
<div class="upload">
<img :src="previewImage ? previewImage : 'assets/images/defaultAva.png'" class="profileImg" />
<label class="edit">
<input ref="imageInput" type="file" name="file" accept="image/png, image/jpeg, image/jpg" @change="uploadImage">
</label>
</div>
<script>
data: () => ({
previewImage: null,
}),
computed: {
getPhoto() {
return this.$store.getters["photoUpload/getPhoto"];
},
},
methods: {
uploadImage(e){
const image = e.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(image);
reader.onload = e =>{
this.previewImage = e.target.result;
};
},
...mapActions("testings", ["getPhoto"]),
},
</script>
So in witch way i must to go, i can upload image to previewImage, but how can i send it to store, to save it locally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
商店的目的是在本地将数据保存到所有应用程序中,而不是在页面刷新中。
来自文档
最常见的情况是将数据从您的API保存到全局变量中,可以在所有应用程序中使用
getters
和您一样。如果要保留重新加载的数据,则可以使用其他软件包,例如 vuex-persist 。
在您的情况下,如果要将图像保存到商店,则可以使用
mapAction
中的操作。这是我的做法:
我使用这些动作:
The purpose of the store is to save data betweens components locally to all the application but not on page refresh.
From the documentation
The most common case is to save data from your api into global variables that can be used in all the application using
getters
as you did.If you want to keep data on reload you can use other packages like vuex-persist.
In your case if you want to save image to the store you can just use the actions from the
mapAction
.Here is how I do it :
And I use the actions as this :