ci010 / electron-vue-next

A starter template for using vue-next with the electron.
https://ci010.github.io/electron-vue-next/
191 stars 27 forks source link

在FooService.ts里添加一个新的方法hello,在useService('FooService')时,报错:Property 'hello' does not exist on type 'FooService'. #54

Closed coder-huo closed 3 years ago

coder-huo commented 3 years ago

在FooService.ts里添加一个新的方法hello,在useService('FooService')时,报错:Property 'hello' does not exist on type 'FooService'.

执行没有问题,就是vscode会报错。 需要添加什么type definition吗?

新增一个BarService, 也会报错:Argument of type '"BarService"' is not assignable to parameter of type 'keyof Services'

ci010 commented 3 years ago

关于在已有service下新添加的东西typescript报错……我试了一下,居然也有,这个我怀疑是vetur的bug……你可以试试重启 vetur 的 restart vls这个命令,重启vetur的language server,然后就好了orz。我得研究研究到底咋回事。

新增一个service是需要在 main/services/index.ts 里面的 interface Services 下面添加的,这个文档里有详细记载

coder-huo commented 3 years ago

新增一个BarService, 也会报错:Argument of type '"BarService"' is not assignable to parameter of type 'keyof Services' 已经在main/services/index.ts 里面的 interface Services 下面添加了BarService

export interface Services { FooService: FooService, BaseService: BaseService, BarService: BarService }

export function initialize(logger: Logger) { _initialize({ BaseService: new BaseService(logger), FooService: new FooService(logger), BarService: new BarService(logger) }) }

ci010 commented 3 years ago

新增一个BarService, 也会报错:Argument of type '"BarService"' is not assignable to parameter of type 'keyof Services' 已经在main/services/index.ts 里面的 interface Services 下面添加了BarService

export interface Services { FooService: FooService, BaseService: BaseService, BarService: BarService }

export function initialize(logger: Logger) { _initialize({ BaseService: new BaseService(logger), FooService: new FooService(logger), BarService: new BarService(logger) }) }

你新增完了重启vetur vls试试……这个应该是vetur的问题,不过我得先看看有什么workaround么……这个太蛋疼了,之前vetur没这个问题

而且你可以直接在 renderer 的某个 ts 里 useSerivce('BarService''),这个应该是直接 work 的,毕竟 vls 只管 vue 文件

ci010 commented 3 years ago

现在有个 workaround,就是退回以前的配置,你在你的项目根目录下放一个 tsconfig.json,内容为:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "downlevelIteration": true,
    "resolveJsonModule": true,
    "strict": true,
    "moduleResolution": "node",
    "baseUrl": ".",
    "paths": {
      "/@shared/*": [
        "./src/shared/*"
      ],
      "/@/*": [
        "./src/renderer/*"
      ],
      "/@main/*": [
        "./src/main/*"
      ],
    },
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules"
  ]
}

然后把 vetur.config.js 给删了。这样你每次改 main 的时候 vetur 也应该更新了

ci010 commented 3 years ago

我感觉我是需要把这个配置revert掉了

coder-huo commented 3 years ago

现在有个 workaround,就是退回以前的配置,你在你的项目根目录下放一个 tsconfig.json,内容为:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "downlevelIteration": true,
    "resolveJsonModule": true,
    "strict": true,
    "moduleResolution": "node",
    "baseUrl": ".",
    "paths": {
      "/@shared/*": [
        "./src/shared/*"
      ],
      "/@/*": [
        "./src/renderer/*"
      ],
      "/@main/*": [
        "./src/main/*"
      ],
    },
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules"
  ]
}

然后把 vetur.config.js 给删了。这样你每次改 main 的时候 vetur 也应该更新了

这样可以。 谢谢。

ci010 commented 3 years ago

现在切换到 volar 上了……对vue3的支持很不错,应该不会有这种问题了