网站搜索

typescript - 如何在根 Vue 组件中正确使用 Pinia 存储?

我的 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,但是当我运行 dev 命令时,会出现 getActivePinia called with no active Pinia. Did you forget to install Pinia 错误。它的行为非常奇怪,因为如果我在开发服务器加载后再使用 useNotificationStore,它运行良好;但是当 useNotificationStore 存在于我的 ts 文件中并运行 dev 命令时,它就会报错。

阅读更多 →

vue.js - 在 Vue3 和 TypeScript 中将 Pinia Store 作为 Prop 传递

我正在使用 Typescript、Pinia 和 Vue3,并且有一个 MenuButton 组件,我希望能够传入一个 Pinia store,该 store 用于菜单的打开状态,以及用于显示/隐藏的操作。应用中有几个不同的菜单,因此我希望能够传入它们,并且它们都使用相同的工厂来定义 store。我正在尝试弄清楚如何让这一切在 TypeScript 中正常工作。

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

阅读更多 →

typescript - 如何使用在 Pinia 存储中定义的数组?

我创建了一个商店以便使用 User 资源,该商店有一个角色数组。我尝试做的是在该数组中搜索特定的角色。我尝试使用 Array 函数,但它不能与 PropType 一起使用。

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

阅读更多 →

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.

阅读更多 →

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/tailwindcs

阅读更多 →

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[], }), });

无论在组件中何处导入此 store,useDataStore() 的返回类型都是 any,这不是类型安全的。我如何

阅读更多 →

typescript - 基于接口定义 pinia 状态,而无需手动重新创建每个属性

我正在尝试在 SPA 中实现状态管理(pinia)。我们正在使用 TypeScript,我想知道是否有任何方法可以根据接口定义状态,而不需要在状态中再次为每个属性进行类型定义。基本上,就是告诉状态它基于某个接口,并且它会以正确的结构进行初始化。

我可以使用 defineStore<string, MyInterface> 定义一个状态以遵循特定的接口结构(这已经是一个很大的进步),但我想知道是否可以遵循展开运算符的概念,以避免在状态声明中输入接口的所有属性。

概念示

阅读更多 →