網站搜尋

typescript - 如何在根 vue 元件中正確使用 Pinia store?

我的 main.ts:

import './assets/tailwind.css' import './assets/wordpress.css' 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')

閱讀更多 →

vue.js - 在使用 Pinia 和 TypeScript 時,如何使用 Action 來設置 State?

我有一個名為 user.ts 的 Pinia + TypeScript 儲存庫,看起來像這樣:

import { User } from 'firebase/auth'; import { defineStore } from 'pinia'; export const useUserStore = defineStore('user', { state: () => ({ displayName: null, email: null

閱讀更多 →

javascript - 在組件外使用 pinia 會引發沒有活動 pinia 的錯誤

我正在使用 ViteSse 模板構建的一個 Vue.js 3 項目中工作,我想在 setup 組件之外使用我其中一個名為 notificationStore 的 Pinia store,但當我運行開發命令時,出現了 getActivePinia called with no active Pinia. Did you forget to install Pinia 錯誤。它的行為非常奇怪,因為如果我在開發服務器加載後再使用 useNotificationStore,則不會出現此錯誤,並且可以正常工作,但當 useNotificationStore 存在於我的 ts 文件中並運行開發命令時,就會引發錯誤。

閱讀更多 →

vue.js - 在 Vue3 和 TypeScript 中將 Pinia 儲存作為 Prop 傳遞

我正在使用 Typescript、Pinia 和 Vue3,並且有一個 MenuButton 組件,我希望能夠傳入一個 Pinia store,用於控制菜單的開啟狀態,以及顯示/隱藏的操作。應用中有幾個不同的菜單,因此我希望能夠將它們傳入,並且它們都使用相同的工廠來定義 store。我正在嘗試弄清楚如何讓這一切在 Typescript 中運作。

// nav.store.ts import { defineStore } from "pinia"; import { useStorage } from "@vu

閱讀更多 →

typescript - 如何使用在 Pinia 商店中定義的陣列?

我創建了一個存儲以便使用 User 資源,該存儲有一個角色陣列。我想做的是在該陣列中搜尋特定的角色。我嘗試使用陣列函數,但它無法與 PropType 一起使用。

import router from "@/router"; import axios from 'axios'; import { defineStore } from "pinia"; import { PropType } from "vue"; import { ApplicationConstants } from '../utils/Constants'; type Role =

閱讀更多 →

typescript - ts(2769) 在 Pinia 儲存中對 Ref 使用 $patch 時沒有符合的重載呼叫

我在 TypeScript 問題上卡住了。程式碼其實是可以運作的,只是 IDE 對我使用 pinia 的 $patch 函數時拋出了錯誤。

這是我的商店:

import { AuthenticationResult } from '@azure/msal-browser' import { defineStore } from 'pinia' import { ref } from 'vue' const useAuthStore = defineStore('auth', () => { const details = ref<AuthenticationResult>() return { details } }) export default useAuthStore

這是我嘗試修改 details 屬性的程式碼:

const tokenResponse: AuthenticationResult = await instance.acquir

閱讀更多 →

typescript - 在使用工廠模式時,Pinia 失去了它的類型

為了我的專案需求,我需要建立一個 storeFactory 來調節 pinia store 的創建。

我特意在這裡移除了參數並簡化了程式範例。

當我在檔案 A 時,storeFactory 的類型會自動正確地被取得

當我處理檔案 B 時,類型丟失,只能推測這個

StoreDefinition<"store_id", {} | _UnwrapAll<Pick<any, string | number>>, {} | Pick<...>, {} | Pick<...>

檔案 A

export function storeFactory() { const baseEntityComponents = {} const extensionComponents = {} return defineStore(`stor

閱讀更多 →

typescript - Nuxt Pinia:無法從非 EcmaScript 模組導入命名匯出 'computed'(僅可使用預設匯出)

我無法在 Nuxt 2.15.8 和 Vue 2.7.10 中使用 Pinia 與 TypeScript,我嘗試了很多方法並安裝了很多依賴,但它就是不工作,我嘗試了很多方法以至於不得不重新啟動我在 GitHub 上的主要資料夾;我安裝了這些依賴

"@pinia/nuxt": "^0.4.9", "vue-server-renderer": "^2.7.10", "vue-template-compiler": "^2.7.10"

在內建模組中,我嘗試使用 Pinia,但它不起作用,所以現在是空的:

buildModules: [ // https://go.nuxtjs.dev/typescript '@nuxtjs/fontawesome', '@nuxt/typescript-build', '@nuxtjs/tailwindc

閱讀更多 →

typescript - Pinia useStore 回傳 any

我正在使用 Vue3 和 TypeScript 的 Pinia。在使用多種不同方式對 store 進行類型定義時,我無法將 store 定義為任何其他類型,除了 any。

以下是實作:

import { defineStore } from "pinia"; import type { DataT } from "@/types"; interface DataStore { data: DataT[]; } export const useDataStore = defineStore("dataStore", { state: (): DataStore => ({ data: [] as DataT[], }), });

無論這個商店在組件中被引入到哪裡,useDataStore() 的返回類型都是 any,這不是類型安全的。我如何才能從

閱讀更多 →

typescript - 根據介面定義 pinia 狀態,而不需要手動再次創建每個屬性

我正在嘗試在單頁應用程式(SPA)中實現狀態管理(pinia)。我們使用 TypeScript,我想知道是否有辦法根據介面定義狀態,而不需要再次為狀態中的每個屬性單獨指定類型。基本上,就是告訴狀態它是基於某個介面,並且使用正確的結構進行初始化。

我可以使用 defineStore<string, MyInterface> 定義一個遵循特定介面結構的 state(這已經是一個很大的進步),但我想知道是否有可能遵循展開運算子的概念,以避免在 state 宣告中輸入介面的所有屬性。

閱讀更多 →