如何存储和保存数据

发布于 2025-02-01 07:34:26 字数 1311 浏览 0 评论 0原文

我开始学习商店,我想上传图像,并将其保存在本地上。因此,如果我重新加载页面映像必须保存

存储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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-02-08 07:34:26

因此,如果我重新加载页面图像必须保存

商店的目的是在本地将数据保存到所有应用程序中,而不是在页面刷新中。

来自文档

“商店”基本上是一个容纳您的应用程序状态

的容器

最常见的情况是将数据从您的API保存到全局变量中,可以在所有应用程序中使用getters和您一样。

如果要保留重新加载的数据,则可以使用其他软件包,例如 vuex-persist


在您的情况下,如果要将图像保存到商店,则可以使用mapAction中的操作。

这是我的做法:

methods: {
  uploadImage(e){
   const image = e.target.files[0];
   const reader = new FileReader();
   reader.readAsDataURL(image);
   reader.onload = e =>{
     this.previewImage = e.target.result;
     this.getPhotoToStore(e.target.result) // <-- here 
   };
  },
  ...mapActions({
      getPhotoToStore: "myStore/getPhoto"
  }),
 },

我使用这些动作:

const actions = {
  getPhoto({ commit }, photo) {
    commit('getPhoto', photo) // <-- commiting to the mutations
  }
}

So if i reloaded page image must be saved

The purpose of the store is to save data betweens components locally to all the application but not on page refresh.

From the documentation

A "store" is basically a container that holds your application state

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 :

methods: {
  uploadImage(e){
   const image = e.target.files[0];
   const reader = new FileReader();
   reader.readAsDataURL(image);
   reader.onload = e =>{
     this.previewImage = e.target.result;
     this.getPhotoToStore(e.target.result) // <-- here 
   };
  },
  ...mapActions({
      getPhotoToStore: "myStore/getPhoto"
  }),
 },

And I use the actions as this :

const actions = {
  getPhoto({ commit }, photo) {
    commit('getPhoto', photo) // <-- commiting to the mutations
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文