Vue3 + Vite + Pinia + 新手学Vue,难以使存储功能正常工作

huangapple go评论61阅读模式
英文:

Vue3 + Vite + Pinia + new to Vue, having a hard time making the store functionalities work

问题

I understand you want a translation of the code and related information. Here is the translated code without any additional information:

src/main.js

import './assets/main.css';
import './scss/styles.scss';
import * as bootstrap from 'bootstrap';

import { createApp } from 'vue';
import { createPinia } from 'pinia';

import App from './App.vue';
import router from './router';

const app = createApp(App);

app.use(createPinia());
app.use(router);

app.mount('#app');

src/stores/incidentStore.js

import { ref, computed } from 'vue';
import { defineStore } from 'pinia';

export const useIncidentStore = defineStore('incidentData', {
  state: () => ({
    ALL_DATA: {},
    selectedCity: '',
    selectedLocation: '',
    selectedReason: '',
    selectedCondition: '',
    selectedType: '',
  }),
  getters: {},
  actions: {
    setData(data) {
      this.ALL_DATA = data;
    },
    setCity(city) {
      this.selectedCity = city;
    },
    setLocation(location) {
      this.selectedLocation = location;
    },
    setReason(reason) {
      this.selectedReason = reason;
    },
    setCondition(condition) {
      this.selectedCondition = condition;
    },
    setType(type) {
      this.selectedType = type;
    },
  },
});

SomeComponent.vue

<script setup>
import { useIncidentStore } from '../stores/IncidentStore';
const store = useIncidentStore();
</script>

<template>
  <form>
    <div class="form-group">
      <label for="i_city">City</label>
      <select
        v-model="city"
        class="form-control"
        id="i_city"
        @change="($event) => store.setCity(city)"
      >
        <option v-for="y in dropdownData.cities" :value="y">
          {{ y }}
        </option>
      </select>
    </div>
  </form>
</template>

<script>
export default {
  data() {
    return {
      city: null,
      dropdownData: {
        cities: [],
      },
    };
  },
  mounted() {
    this.fetchDropdownData();
  },
  methods: {
    fetchDropdownData() {
      const re = /\bIn|East of|West of|South of|North of|North West of|South West of|North East of|South East of|Near/gi;
      fetch(`${import.meta.env.VITE_API_VERBOSE}`)
        .then((response) => {
          if (!response.ok) {
            throw new Error('Network response was not ok');
          }
          return response.json();
        })
        .then((data) => {
          this.dropdownData = {
            cities: new Set(data.map((el) => el.city.substring(0, 50)).sort()),
          };
          console.log('this.dropDownData', this.dropdownData);
        })
        .catch((error) => {
          console.error('Error:', error);
        });
    },
    getData() {
      console.log('selected_city', store.selectedCity);
    },
  },
};
</script>

I hope this helps. If you have any further questions or need additional assistance, please let me know.

英文:

Having trouble making Pinia work following the documentation and cheat sheet. It doesn't say WHERE to put WHAT and HOW. So I'm a bit lost, and newish to Vue too.

I'm trying to set up a series of dropdowns (I'm just including "city" here) and a Google map that can take that data and display it. But I can't get the store working.

Just a little note, please remember that other people have different learning styles and some, like me, have learning disabilities. So what might seem like a "dumb" question might be challenging with someone with my disabilities. Thanks!

src/main.js

import &#39;./assets/main.css&#39;
import &#39;./scss/styles.scss&#39;
import * as bootstrap from &#39;bootstrap&#39;

import { createApp } from &#39;vue&#39;
import { createPinia } from &#39;pinia&#39;

import App from &#39;./App.vue&#39;
import router from &#39;./router&#39;

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.mount(&#39;#app&#39;)

src/stores/incidentStore.js

    import { ref, computed } from &#39;vue&#39;
    import { defineStore } from &#39;pinia&#39;

export const useIncidentStore = defineStore(&#39;incidentData&#39;, {
  state: () =&gt; ({
    ALL_DATA: {},
    selectedCity: &#39;&#39;,
    selectedLocation: &#39;&#39;,
    selectedReason: &#39;&#39;,
    selectedCondition: &#39;&#39;,
    selectedType: &#39;&#39;
  }),
  getters: {},
  actions: {
    setData(data) {
      this.ALL_DATA = data
    },
    setCity(city) {
      this.selectedCity = city
    },
    setLocation(location) {
      this.selectedLocation = location
    },
    setReason(reason) {
      this.selectedReason = reason
    },
    setCondition(condition) {
      this.selectedCondition = condition
    },
    setType(type) {
      this.selectedType = type
    }
  }
})

SomeComponent.vue

&lt;script setup&gt;
import { useIncidentStore } from &#39;../stores/IncidentStore&#39;
const store = useIncidentStore()
&lt;/script&gt;

&lt;template&gt;
  &lt;form&gt;
    &lt;div class=&quot;form-group&quot;&gt;
      &lt;label for=&quot;i_city&quot;&gt;City&lt;/label&gt;
      &lt;select
        v-model=&quot;city&quot;
        class=&quot;form-control&quot;
        id=&quot;i_city&quot;
        @change=&quot;($event) =&gt; store.setCity(city)&quot;
      &gt;
        &lt;option v-for=&quot;y in dropdownData.cities&quot; :value=&quot;y&quot;&gt;
          {{ y }}
        &lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;/form&gt;
 &lt;/template&gt;


&lt;script&gt;
export default {
  data() {
    return {
      city: null
      dropdownData: {
        cities: []
      }
    }
  },
  mounted() {
    this.fetchDropdownData()
  },
  methods: {
 
    fetchDropdownData() {
      const re =
        /\bIn|East of|West of|South of|North of|North West of|South West of|North East of|South East of|Near/gi
      fetch(`${import.meta.env.VITE_API_VERBOSE}`)
        .then((response) =&gt; {
          if (!response.ok) {
            throw new Error(&#39;Network response was not ok&#39;)
          }
          return response.json()
        })
        .then((data) =&gt; {
          // Create a new reactive object using Vue.set or spread operator
          this.dropdownData = {
            cities: new Set(data.map((el) =&gt; el.city.substring(0, 50)).sort())
          }
          console.log(&#39;this.dropDownData&#39;, this.dropdownData)
        })
        .catch((error) =&gt; {
          // Handle error
          console.error(&#39;Error:&#39;, error)
        })
    },
    getData() {
    
      console.log(&#39;selected_city&#39;, store.selectedCity)
    }
  }
}
&lt;/script&gt;

ERRORS:
Vue3 + Vite + Pinia + 新手学Vue,难以使存储功能正常工作

Literally no way that I try to set this up following these bizarro instructions make this work as advertised. How is this "easier" than VUEX??

So what do I need to do to get the dropdown menu to set the city and then show it in the console log at the bottom in getData()?

So, first error is solved, probably, but second error now says that &quot;store is not defined&quot; in the method getData(). Is there some magic trick I need to actually display the data? Thanks!

Vue3 + Vite + Pinia + 新手学Vue,难以使存储功能正常工作

答案1

得分: 3

这次尝试在这里调用 store.setCityValue() 的操作不起作用,因为存储中没有名为 setCityValue 的操作。

有两个名为 setCity 的操作。我认为可能第一个只需要将其名称更改为 setCityValue

英文:

This attempt to call store.setCityValue() here:

@change=&quot;($event) =&gt; store.setCityValue(city)&quot;

does not work because there is no action on the store called setCityValue

actions: {
    setData(data) {
      this.ALL_DATA = data
    },
    setCity(city) {
      this.selectedCity = city
    },
    setLocation(location) {
      this.selectedLocation = location
    },
    setCity(reason) {
      this.selectedReason = reason
    },
    setCondition(condition) {
      this.selectedCondition = condition
    },
    setType(type) {
      this.selectedType = type
    }
  }

There are two actions called setCity. I think maybe the first one just needs it's name changed to setCityValue

huangapple
  • 本文由 发表于 2023年6月6日 05:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定