为什么 stores pinia 没有定义?

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

Why the stores pinia is not defined?

问题

I'm expecting to return the store but I'm having an error useUsersStore is not defined. Even though the pinia store is correctly stored and exported to the component.

https://stackblitz.com/edit/vitejs-vite-wg4uyx?file=src%2Fcomponents%2FFilters.vue,src%2Fstores%2FusersStore.js,src%2Fmain.js

// Component 
export default {
  setup() {
    const usersStore = useUsersStore();

    const countries = usersStore.getCountries;
    const scores = usersStore.getScores;

    const country = ref('');
    const score = ref('');

    // etc.
  }
}

// main.js
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';

const pinia = createPinia();
const app = createApp(App);

app.use(pinia);
app.mount('#app');
英文:

I'm expecting to return the store but I'm havin an error useUsersStore is not defined. Even though the pi nia store is corresctly stores and exported to th ecomponent.

https://stackblitz.com/edit/vitejs-vite-wg4uyx?file=src%2Fcomponents%2FFilters.vue,src%2Fstores%2FusersStore.js,src%2Fmain.js

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  import { defineStore } from &#39;pinia&#39;;

    export const useUsersStore = defineStore(&#39;usersStore&#39;, {
      state: () =&gt; ({
      ...
      }}
      
      // Component 
      
      export default {
      setup() {
        const usersStore = useUsersStore();

        const countries = usersStore.getCountries;
        const scores = usersStore.getScores;

        const country = ref(&#39;&#39;);
        const score = ref(&#39;&#39;);
        
        etc.
        
        }}

// main.js

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

const pinia = createPinia();
const app = createApp(App);

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

<!-- end snippet -->

答案1

得分: 1

“我遇到了一个错误,useUsersStore未定义”,当然你会遇到这个错误,因为你还没有导入它。

#### `Filters.vue`
```javascript
&lt;script&gt;
import { ref } from 'vue';

// 添加这个导入语句
import { useUsersStore } from '../stores/usersStore';

export default {
...
英文:

"I'm havin an error useUsersStore is not defined", Of course you will get the error, as you haven't imported it.

Filters.vue

&lt;script&gt;
import { ref } from &#39;vue&#39;;

// Add this import statement
import { useUsersStore } from &#39;../stores/usersStore&#39;;

export default {
...

huangapple
  • 本文由 发表于 2023年5月29日 00:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352541.html
匿名

发表评论

匿名网友

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

确定