بنقرة واحدة
vue2-setup
将 Vue 2 Class 组件迁移到 Vue 2.7 Setup 格式。当用户需要迁移 vue-class-component 代码到 Composition-API 时使用。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
将 Vue 2 Class 组件迁移到 Vue 2.7 Setup 格式。当用户需要迁移 vue-class-component 代码到 Composition-API 时使用。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
| name | vue2-setup |
| description | 将 Vue 2 Class 组件迁移到 Vue 2.7 Setup 格式。当用户需要迁移 vue-class-component 代码到 Composition-API 时使用。 |
文件最开始使用vue@2格式的class方案书写,现在需要迁移为vue@2.7的setup格式,项目全部使用vue@2.7
代码setup格式参考如下示例文件格式
animal-world/platforms/aweb-config/src/views/InLevelActivitySnake/InLevelActivityPreview/InLevelActivityPreview.vue
重要:必须保留原始代码中的所有注释。
import { reactive, toRefs } from 'vue'
const state = reactive({
loading: false,
data: [],
})
return {
...toRefs(state),
// 其他方法
}
使用 @aweb/pkg-common 的 useContext 替代 this
import { useContext } from '@aweb/pkg-common'
const { $route, $router, $message } = useContext()
只返回模板中实际使用的值
按以下顺序组织 import:
示例:
import { reactive, computed, onMounted } from 'vue'
import { useContext } from '@aweb/pkg-common'
import { someUtil } from '@/utils/helper'
使用 Vue 提供的 PropType 为复杂类型的 props 添加 TypeScript 类型支持:
示例:
import { PropType } from 'vue'
props: {
// 数组类型
items: {
type: Array as PropType<number[]>, default: () => [],
},
// 对象类型
config: {
type: Object as PropType<{ name: string; value: number }>, default: () => ({}),
},
}
tsx语法
可以从useContext中获取h方法,参考示例:animal-world/platforms/aweb-config/src/components/selector/platformV2/PlatformSelectorV2.vue注意:不要使用 Array as () => number[] 这种写法,应使用 PropType<T> 更加清晰规范。
vue-class-component 和 vue-property-decorator 导入useContext() 替代 this.$route/$router/$message...toRefs(state) 导出this. 已移除PropType<T> 定义类型