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了

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. AIApe问答机器人Scrum Meeting 4.25

    Scrum Meeting 2 日期:2021年4月25日 会议主要内容概述:前后端针对WebAPI进行协调与统一工作,商量接下来两日计划:敲定部分设计细节. 一.进度情况 组员 负责 两日内已完成的 ...

  2. Sharding-JDBC自定义复合分片算法

    Sharding-JDBC自定义复合分片算法 一.背景 二.需求 1.对于客户端操作而言 2.对于运营端操作而言 三.分片算法 1.客户id和订单id的生成规则 2. 确定数据落在那个表中 3.举例说 ...

  3. sql_exporter的使用

    sql_exporter的使用 一.背景 二.sql-exporter的使用 1.下载 2.配置文件 1.sql_exporter.yml 2.collectors 目录中的配置文件 1.collec ...

  4. 热身训练1 Blood Cousins Return

    点此看题 简要题面: 一棵树上有n个节点,每个节点有对应的名字(名字可重复). 每次询问,求深度比$vi$多$ki$的$vi$的儿子中,有多少种名字 分析: Step1: 我们可以懂$DFS$轻松找到 ...

  5. Android编译执行envsetup.sh,产生工具命令m、mm、mmm、mmma、tapas 、croot、cgrep、jgrep、 resgrep、godir

    一般来说编译一个sdk或者一个比较大的工程项目,第一步都是执行 envsetup.sh这个脚本,比如编译android,qt源码以及其他一些嵌入式的sdk. 而且执行的时候需要特别注意使用 sourc ...

  6. hdu 2201 熊猫阿波的故事(简单概率。。)

    题意: 阿波上了飞机,飞机上有座位1,2,....,N.第i个乘客本应坐在第i个座位上. 可是阿波随便找了个座位就坐了下来,接下来大家也都随便找了个座位坐了下来. 问:第i个乘客坐到原座位的概率是多少 ...

  7. DeWeb --- Hello,World!

    1.新建一个DLL,命名为hello.dpr 2.新增一个Form.(File->New->VCL Form - Delphi),建议不要更改单元名称和Form名称,即分别为unit1.p ...

  8. lamp 架构的理解

    1,lamp架构下的求情过程如下: 2,httpd服务器连接php服务器的三种方式 3,php和mysql的连接

  9. nginx + tomcat 实现负载均衡

    1.环境准备 服务器A上安装 nginx 作为代理服务器 服务器B上安装 tomcat,~/webapps 下创建 /test目录,创建 /index.html 内容为T1(生产环境中一般是一样的wa ...

  10. 记一次线上环境 ES 主分片为分配故障

    故障前提 ElasticSearch 版本:5.2 集群节点数:5 索引主分片数:5 索引分片副本数:1 线上环境ES存储的数据量很大,当天由于存储故障,导致一时间 5个节点的 ES 集群,同时有两个 ...