typescript - Vue3 Plugin Typescript Typings
问题描述
I'm trying to write a plugin with Vue3 in Typescript. However, I'm stuck from the start with how to properly type the plugin function.
This is what I have so far:
import MyComponent from './src/components/my-component.vue'
import { DefineComponent, Plugin } from 'vue'
const plugin: Plugin = {
install (app, options?: { [key: string]: any }) {
app.mixin({
computed: {
classes (this: DefineComponent) {
// ...do things to `this`
}
}
})
app.component('MyComponent', MyComponent)
}
}
export default plugin
One of the biggest problems is how to deal with this
in the context of these functions. There's just red all over the screen at the moment.
I'm trying to use rollup
for bundling.
This shows no errors, but when imported into another Vue project it blows up.
How to properly type a plugin function using Typescript, Vue3 and Rollup?
解决方案
This is not really an answer, but the above code does actually work.
After deleting node_modules
and reinstalling, everything worked.
I'm leaving this question up because it took a while to find Plugin
.
import { DefineComponent, Plugin } from 'vue'
const plugin: Plugin = {
install (app, options?) {
app.mixin({
computed: {
classes (this: DefineComponent) {
// Do stuff here. Even with `this`
}
}
})
}
}
Using this
as the first argument of a function tells the Typescript compiler what this
actually is.
推荐阅读
- sql - 更新或删除违反外键约束
- javascript - “如何将项目添加到列表中,然后添加到下面的空白处?”
- python - 如何将字典中冻结的键和值集转换为普通键和值?
- amazon-web-services - AWS Glue ETL 和 PySpark 以及分区数据:如何从分区创建数据框列
- excel - 如何按列名查找列字母以在 vba 的 SUM 公式中使用?
- java - Java 流收集计数到字段
- c - 如何将优化的 x86-64 asm 循环转换回 C for 循环?
- amazon-web-services - 将 AWS Secrets Manager 密钥注入 API Gateway 集成
- assembly - 如何找出blx分支的位置
- asp.net-mvc - 从使用 .Net Classic 中的 ninject 迁移到 .Net Core