在vue项目中使用TS

在vue项目中使用TS

1. 将vue项目注入ts 引入和使用

webpack的打包配置:vue-cli webnpack 编译时

entry 入口 设置

entry: {
    app: './src/maikn.ts'
}

2. resolve: extensions 添加 ts 用于处理尝试的数据尾缀列表

  • 问: 如何在webpack新增处理类型文件: resolve配置项的extension里添加ts文件

TS 配置文件: typeScript.ts

vue/vuex + ts

1. 定义组件的方式上: 形式上:

import vue from 'vue'
const Compoinent = Vue.extend({
    // 类型
})

2. 注入实例: 使用官方工具:vue-class-component

  • 相当于给vue 添加了一个类装饰器的能力 从而实现实例 的注入
 import Component from 'vue-class-component'
 export default class myComponent extends Vue {
     message: string = 'hello'
     onClick(): void {
         console.log(this.message)
     }
 }

3. 独立模块的声明: 利用 ts的额外补充模块 declare实现 使之可以被独立引用

declare module '*.vue' {
    import Vue from 'vue'
    export default vue
}
declare module 'typings/vuePlugin.d.ts'{
    interface Vue {
        myProps: string
    }
}
// 实例中使用
let vm = new Vue()
console.log(vm.myProps)

4. props = 提供propType原地声明联合变量

import { propType } from 'vue'
interface customPayload {
    str: string,
    number: number,
    name: string
}

const  Component = Vue.extend({
    pros: {
        name: String,
        success: {
            type: String
        },
        payLoad: {
            type: Object as propType<customPayload>
        },
        callback: {
            type: Function as propType<() => void>
        }
    }
})

5. computed 和 methods

computed: {
    getMsg():string {
        return this.click() + '!'
    }
},
methods: {
    click(): string {
        return this.message + 'zhaowa'
    }
}

6. vuex的接入 - 声明使用

// vuex.d.ts声明模块 - ComponentCustomProperties
declare module '@vue/runtime-core' {
    interface State {
        count: number
    }
    interface ComponemtCustomProperties {
        $store: Store<State>
    }
}

7. api形式编码实现

// store.ts - 状态机侧
import { InjectionKey } from 'vue'
import {
    createStore,
    Store
} from 'vuex'
export interface State {
    count: number;
}
export const key: InjectionKey<Store<State>> = Symbol()
export const store = createStore<State>({
    state: {
        count: 0
    }
})
// main.ts - 入口侧代码
import { createApp } from 'vue'
import { store, key } form './store'
const app = createApp({
    // 参数
})
// 利用了provide。inject
app.use(store, key)  
//  => 传入injectionkey 
app.mount('#app')

//  消费方
import { useStore } from 'vuex'
import { key } from './store'
export default {
    const store = useStore(key)
    // store.state.count
}

8. vuex面向对象 (装饰器 结构更加统一) - 使用vuex-class 工具

import { State. Action, Getter } from "vuex-class"
export defaulty class App extends Vue {
    // 利用属性装饰器整合store的状态
    @store login: boolean
    // 利用事件装饰器整合store方法
    @Action setInit: ()=> void

    get isLogin: boolean
    mounted() {
        this.setInit()
        this.isLogin = this.login
    }
}

相关推荐

  1. vue项目使用TS

    2024-04-25 19:42:04       31 阅读
  2. vue项目使用tsx

    2024-04-25 19:42:04       32 阅读
  3. 【菜狗学前端】 初探TS(结合vue3使用

    2024-04-25 19:42:04       32 阅读
  4. Vue3 + Vite项目使用less

    2024-04-25 19:42:04       55 阅读
  5. vue项目封装并使用WebSocket(2)

    2024-04-25 19:42:04       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-25 19:42:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-25 19:42:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-25 19:42:04       82 阅读
  4. Python语言-面向对象

    2024-04-25 19:42:04       91 阅读

热门阅读

  1. el-table 表格自带全选按钮隐藏

    2024-04-25 19:42:04       25 阅读
  2. flask + celery + redis + flower

    2024-04-25 19:42:04       33 阅读
  3. C 语言实例 - 字符转 ASCII 码

    2024-04-25 19:42:04       32 阅读
  4. HashMap 和 HashTable的异同

    2024-04-25 19:42:04       32 阅读
  5. Linux c++ 中文字符转十六进制 UTF-8 编码

    2024-04-25 19:42:04       23 阅读
  6. 【React】生命周期

    2024-04-25 19:42:04       31 阅读
  7. 【vue】axios封装拦截

    2024-04-25 19:42:04       27 阅读
  8. Electron vue 进程间消息通行

    2024-04-25 19:42:04       34 阅读