gpt4 book ai didi

typescript - 如何在使用 Vue CLI@3 和 TypeScript 设置的项目中使用?

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:03 25 4
gpt4 key购买 nike

td;dr如何在已使用 TypeScript 的项目中包含 vue-apollo?

我使用 vue cli@3 和 TS 创建了一个新的 vue 项目。

然后我添加了 vue-apollo 插件,它修改了我的 main.ts 文件以添加

apolloProvider: createProvider(),

在vue实例创建中。

但是编译器对此有提示。

Argument of type '{ router: VueRouter; store: Store<{}>; apolloProvider: { provide: () => {}; }; render: (h: CreateElement) => VNode; }' is not assignable to parameter of type 'ComponentOptions, DefaultMethods, DefaultComputed, PropsDefinition>, Record>'.

Object literal may only specify known properties, and 'apolloProvider' does not exist in type 'ComponentOptions, DefaultMethods, DefaultComputed, PropsDefinition>, Record>'. [2345]

我可以看到在我从 npm 获取的 vue-apollo 包中有一个 types 目录,但不确定如何使用它们。

我也得到以下信息

[ts] Could not find a declaration file for module '@/vue-apollo'. '/Users/praveen/code/voicezen/repos/voicezen-ui/src/vue-apollo.js' implicitly has an 'any' type. [7016]

对于 main.ts 中的以下导入

import { createProvider } from '@/vue-apollo';

我知道这可能来自 noImplicitAny 规则,但随后将生成的 vue-apollo.js 更改为 vue-apollo.ts 也没有解决问题。

将其更改为 .ts 从 main.ts 中删除关于上述两个的编译器错误,但随后我得到以下信息。

Could not find a declaration file for module 'vue-cli-plugin-apollo/graphql-client'. '/Users/praveen/code/voicezen/repos/voicezen-ui/node_modules/vue-cli-plugin-apollo/graphql-client/index.js' implicitly has an 'any' type.

对于

import {
createApolloClient,
restartWebsockets
} from 'vue-cli-plugin-apollo/graphql-client';

要解决这个问题,我可以像这样在我的输入中添加一个模块声明,但这是正确的方法吗?

declare module 'vue-cli-plugin-apollo/graphql-client';

onLoginonLogout 方法param apolloClient,在生成的vue-apollo.ts 开始报错它们也是隐含的 any 类型。

最佳答案

你在正确的轨道上,将 vue-apollo.js 更改为 .ts

然后在“tsconfig”中添加“vue-cli-plugin-apollo”。

关于typescript - 如何在使用 Vue CLI@3 和 TypeScript 设置的项目中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53331968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com