Vue 中使用 TypeScript 詳細總結


VUE 項目中使用 Typescript

第一節:項目起步

Vue 中使用 TypeScript

  • 項目中主要使用到的第三方依賴
  • vue2
vue-class-component
vue-property-decorator
less
vue-router
vuex
vuex-class

搭建項目

// 按照提示自定義vue選項,我這里使用的是vue2 + ts
vue create pm-vue2-ts-app

// 項目創建成功進入工程目錄啟動項目
npm run server

App.vue 中內容

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
    </div>
    <router-view/>
  </div>
</template>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

main.ts 中配置

import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');

  • 創建第一個組件簡單組件 TsDemo.vue
<template>
  <div>
    <h1>{{ name }}</h1>
    <div>{{ mess }}</div>
    <button @click="addOne">測試按鈕點擊加一{{ num }}</button>
    <button @click="onhanlf">調用父組件事件</button>
  </div>
</template>

<script lang="ts">

// 導入一下選項
import {Component, Emit, Prop, Vue} from 'vue-property-decorator';

@Component
export default class TsDemo extends Vue {
    // props 傳值 定義類型是 string 類型,默認值是 ’message‘
    @Prop({default: 'message'}) private mess!: string;
    // 組件私有變量
    private name: string = 'test demo';
    private num: number = 0;
    // 組件方法 methods 中提供的方法,直接寫在下面
    private addOne() {
        this.num++;
    }
    // 調用父組件方法
    private onhanlf() {
        this.$emit('handle', {});
    }
}
</script>

在About.vue 中引用 TsDemo 組件

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <tsDemo mess="About 父組件" @handle="handle"></tsDemo>
  </div>
</template>

<script lang="ts">
// 引入Component, Vue
import {Component, Vue} from 'vue-property-decorator';
// 引入 tsDemo 組件
import tsDemo from '@/components/TsDemo.vue';

// 注意:在組件中使用路由數位需要提前注冊
Component.registerHooks([
  'beforeRouteLeave',
]);

// 在 Component  中引入組件 tsDemo
@Component({
  components: {
    tsDemo,
  },
})
export default class About extends Vue {
  //  父組件提供方法,在 tsDemo 子組件中調用
  private handle(val: object) {
    console.log(val);
  }
    // 組件中的路由守衛
  private beforeRouteLeave(to: any, from: any, next: any) {
    console.log(to, from);
    next();
  }
}
</script>

路由配置 router/index.ts 文件中配置路由

import Vue from 'vue';
import VueRouter, { RouteConfig } from 'vue-router';
import Home from '../views/Home.vue';

Vue.use(VueRouter);

const routes: RouteConfig[] = [
  {
    path: '/',
    name: 'Home',
    component: Home,
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;

到這里一個簡單的vue + ts 項目中配置就都OK了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM