VUE 项目中使用 Typescript

第一节:项目起步

Vue 中使用 TypeScript

  • 项目中主要使用到的第三方依赖
  • vue2
  1. vue-class-component
  2. vue-property-decorator
  3. less
  4. vue-router
  5. vuex
  6. vuex-class

搭建项目

  1. // 按照提示自定义vue选项,我这里使用的是vue2 + ts
  2. vue create pm-vue2-ts-app
  3. // 项目创建成功进入工程目录启动项目
  4. npm run server

App.vue 中内容

  1. <template>
  2. <div id="app">
  3. <div id="nav">
  4. <router-link to="/">Home</router-link> |
  5. <router-link to="/about">About</router-link>
  6. </div>
  7. <router-view/>
  8. </div>
  9. </template>
  10. <style lang="less">
  11. #app {
  12. font-family: Avenir, Helvetica, Arial, sans-serif;
  13. -webkit-font-smoothing: antialiased;
  14. -moz-osx-font-smoothing: grayscale;
  15. text-align: center;
  16. color: #2c3e50;
  17. }
  18. #nav {
  19. padding: 30px;
  20. a {
  21. font-weight: bold;
  22. color: #2c3e50;
  23. &.router-link-exact-active {
  24. color: #42b983;
  25. }
  26. }
  27. }
  28. </style>

main.ts 中配置

  1. import Vue from 'vue';
  2. import App from './App.vue';
  3. import router from './router';
  4. import store from './store';
  5. Vue.config.productionTip = false;
  6. new Vue({
  7. router,
  8. store,
  9. render: (h) => h(App),
  10. }).$mount('#app');
  • 创建第一个组件简单组件 TsDemo.vue
  1. <template>
  2. <div>
  3. <h1>{{ name }}</h1>
  4. <div>{{ mess }}</div>
  5. <button @click="addOne">测试按钮点击加一{{ num }}</button>
  6. <button @click="onhanlf">调用父组件事件</button>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. // 导入一下选项
  11. import {Component, Emit, Prop, Vue} from 'vue-property-decorator';
  12. @Component
  13. export default class TsDemo extends Vue {
  14. // props 传值 定义类型是 string 类型,默认值是 ’message‘
  15. @Prop({default: 'message'}) private mess!: string;
  16. // 组件私有变量
  17. private name: string = 'test demo';
  18. private num: number = 0;
  19. // 组件方法 methods 中提供的方法,直接写在下面
  20. private addOne() {
  21. this.num++;
  22. }
  23. // 调用父组件方法
  24. private onhanlf() {
  25. this.$emit('handle', {});
  26. }
  27. }
  28. </script>

在About.vue 中引用 TsDemo 组件

  1. <template>
  2. <div class="about">
  3. <h1>This is an about page</h1>
  4. <tsDemo mess="About 父组件" @handle="handle"></tsDemo>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. // 引入Component, Vue
  9. import {Component, Vue} from 'vue-property-decorator';
  10. // 引入 tsDemo 组件
  11. import tsDemo from '@/components/TsDemo.vue';
  12. // 注意:在组件中使用路由数位需要提前注册
  13. Component.registerHooks([
  14. 'beforeRouteLeave',
  15. ]);
  16. // 在 Component 中引入组件 tsDemo
  17. @Component({
  18. components: {
  19. tsDemo,
  20. },
  21. })
  22. export default class About extends Vue {
  23. // 父组件提供方法,在 tsDemo 子组件中调用
  24. private handle(val: object) {
  25. console.log(val);
  26. }
  27. // 组件中的路由守卫
  28. private beforeRouteLeave(to: any, from: any, next: any) {
  29. console.log(to, from);
  30. next();
  31. }
  32. }
  33. </script>

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

  1. import Vue from 'vue';
  2. import VueRouter, { RouteConfig } from 'vue-router';
  3. import Home from '../views/Home.vue';
  4. Vue.use(VueRouter);
  5. const routes: RouteConfig[] = [
  6. {
  7. path: '/',
  8. name: 'Home',
  9. component: Home,
  10. },
  11. {
  12. path: '/about',
  13. name: 'About',
  14. component: () => import('../views/About.vue'),
  15. },
  16. ];
  17. const router = new VueRouter({
  18. mode: 'history',
  19. base: process.env.BASE_URL,
  20. routes,
  21. });
  22. export default router;

到这里一个简单的vue + ts 项目中配置就都OK了

Vue 中使用 TypeScript 详细总结的更多相关文章

  1. 在Vue 中使用Typescript

    Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...

  2. Vue 中使用 typescript

    Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...

  3. Vue 中使用 TypeScript axios 使用方式

    Vue 中使用 TypeScript axios 使用方式 方式一 import axios from 'axios'; Vue.prototype.$axios = axios; // 在 .vue ...

  4. 在 Vue 中使用 Typescript

    前言 恕我直言,用 Typescript 写 Vue 真的很难受,Vue 对 ts 的支持一般,如非万不得已还是别在 Vue 里边用吧,不过听说 Vue3 会增强对 ts 的支持,正式登场之前还是期待 ...

  5. vue中watch的详细用法

    在vue中,使用watch来响应数据的变化.watch的用法大致有三种.下面代码是watch的一种简单的用法: <input type="text" v-model=&quo ...

  6. vue中watch的详细用法(转载)

    在vue中,使用watch来响应数据的变化.watch的用法大致有三种.下面代码是watch的一种简单的用法: <input type="text" v-model=&quo ...

  7. vue中router-link的详细用法

    官网文档地址:https://router.vuejs.org/zh/api/#to 今天项目突然有需求,让vue中的一个页面跳转到另一个页面 // 字符串 <router-link to=&q ...

  8. 在Vue中使用TypeScript

    TypeScript基础学习 初始化TS项目 Vue装饰器 vue-typescript-admin框架 1.TypeScript基础学习 2.初始化TS项目 3.Vue装饰器 Vue装饰器常用的有下 ...

  9. TypeScript基础以及在Vue中的应用

    TypeScript推出已经很长时间了,在Angular项目中开发比较普遍,随着Vue 3.0的即将推出,TypeScript在Vue项目中使用也即将成为很大的趋势,笔者也是最近才开始研究如何在Vue ...

随机推荐

  1. 小白自制Linux开发板 八. Linux音频驱动配置

    不知不觉小白自制开发板系列已经到第八篇了,本篇要配置的是音频驱动,也算是硬件部分的最后一片了,积攒的文章也差不多全放完了,后续更新可能会放缓,还请见谅. 对于F1C200s是自带了多媒体处理功能的,所 ...

  2. FastAPI 学习之路(三十八)Static Files

    如果使用前后台不分离的开发方式,那么模板文件中使用的静态文件,比如css/js等文件的目录需要在后台进行配置,以便模板渲染是能正确读到这些静态文件.那么我们应该如何处理呢. 首先安装依赖 pip in ...

  3. 配置pyenv环境

    git clone https://github.com/pyenv/pyenv.git ~/.pyenv echo 'export PYENV_ROOT="$HOME/.pyenv&quo ...

  4. [no code][scrum meeting] Beta 9

    $( "#cnblogs_post_body" ).catalog() 例会时间:5月23日15:30,主持者:肖思炀 下次例会时间:5月25日11:30,主持者:伦泽标 一.工作 ...

  5. 2021.10.27考试总结[冲刺NOIP模拟17]

    T1 宝藏 发现每个数成为中位数的长度是关于权值单调的.线段树二分判断是否合法,单调指针扫即可. 考场上写了二分,平添\(\log\). \(code:\) T1 #include<bits/s ...

  6. QMake(Qt项目构建)

    qmake工具能够简化不同平台上的项目构建.可以自动产生Makefiles文件,仅仅需要少量的信息就可以生成Makefile文件.同时qmake也可以构建不是Qt的项目.qmake基于项目文件中的信息 ...

  7. (类)Program1.1

    1 class MyClass: 2 3 i = 12345 4 5 def __init__(self): 6 self.data = "WOOWOWOWO" 7 8 def f ...

  8. Django(73)django-debug-toolbar调试工具

    介绍 Django框架的调试工具栏使用django-debug-toolbar库,是一组可配置的面板,显示有关当前请求/响应的各种调试信息,点击时,显示有关面板内容的更多详细信息. 应用 1. 安装 ...

  9. Python技法4:闭包

    闭包:用函数代替类 有时我们会定义只有一个方法(除了__init__()之外)的类,而这种类可以通过使用闭包(closure)来替代.闭包是被外层函数包围的内层函数,它能够获取外层函数范围中的变量(即 ...

  10. (1)Zookeeper在linux环境中搭建集群

    1.简介 ZooKeeper是Apache软件基金会的一个软件项目,它为大型分布式计算提供开源的分布式配置服务.同步服务和命名注册.ZooKeeper的架构通过冗余服务实现高可用性.Zookeeper ...