npm install
npm run serve
npm run build
Vue3.x combined with typescript first experience
vue3.x adopts Function-based API format to organize the code, making it easier to compress the code and the compression efficiency is higher. Due to the modification of the component declaration, the logic is completed in the way of function combination, which is naturally combined with typescript. (The components in vue2.x are passed in a series of options through declarations, so using typeScript under 2.x requires a decorator. vue-class-component
)
// vue2.x 要想使用ts 需要这样处理,详见官方文档 https://cn.vuejs.org/v2/guide/typescript.html
<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
@Component
export default class App extends Vue {}
</script>
npm install -g @vue/cli
vue create vue-next-project
After entering this command, it is necessary to select Manually select features
at least make babel
typescript
router
chosen, ( vuex
see whether their case). As shown below:
I don’t know the reference articles of vue-cli
https://github.com/vuejs/vue-cli-plugin-vue-next
cd vue-next-project
vue add vue-next
This command will automatically help you upgrade vue2.x to vue3.x, including upgrading the corresponding dependencies in the project and replacing the template code, like:vue-router
vuex
After completing the above three steps, the main environment is set up. Seeing that there is an additional tsconfig.json configuration file in the directory just created, you can configure it according to your own and project needs.
Next, we need to deal with it briefly to make it support TypeScript. (The template cli has not yet completed the typescript template code)
shims-vue.d.ts
contents of the file are changed a bit, this operation should be a little something error.// declare module "*.vue" {
// import Vue from 'vue';
// export default Vue;
// }
declare module "*.vue" {
import {defineComponent} from 'vue'
const Component: ReturnType<typeof defineComponent>;
export default Component
}
script lang="ts"
, and then used defineComponent
to wrap it.<script lang="ts">
import {
defineComponent,
ref,
onMounted,
onUpdated,
onUnmounted,
} from "vue";
export default defineComponent({
name: "hello world",
props: {},
setup(props: any) {
const count = ref(0);
const increase = (): void => {
count.value++;
};
function test(x: number): string {
return props.toString();
}
test(1);
// test('number');
// 生命周期
onMounted(() => {
console.log("mounted vue3 typescript");
});
onUpdated(() => {
console.log("updated vue3 typescript");
});
onUnmounted(() => {
console.log("onUnmounted vue3 typescript");
});
// 暴露给模板
return {
count,
increase
};
},
});
</script>
Life cycle correspondence
Combined API corresponding to the 2.x version life cycle
beforeCreate → 使用 setup()
created → 使用 setup()
beforeMount → onBeforeMount
mounted → onMounted
beforeUpdate → onBeforeUpdate
updated → onUpdated
beforeDestroy → onBeforeUnmount
destroyed → onUnmounted
errorCaptured → onErrorCaptured
Author: vue3
Source Code: https://github.com/vue3/vue3-router4-typescript
#vuejs #vue #javascript