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了
Vue 中使用 TypeScript 详细总结的更多相关文章
- 在Vue 中使用Typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...
- Vue 中使用 typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...
- Vue 中使用 TypeScript axios 使用方式
Vue 中使用 TypeScript axios 使用方式 方式一 import axios from 'axios'; Vue.prototype.$axios = axios; // 在 .vue ...
- 在 Vue 中使用 Typescript
前言 恕我直言,用 Typescript 写 Vue 真的很难受,Vue 对 ts 的支持一般,如非万不得已还是别在 Vue 里边用吧,不过听说 Vue3 会增强对 ts 的支持,正式登场之前还是期待 ...
- vue中watch的详细用法
在vue中,使用watch来响应数据的变化.watch的用法大致有三种.下面代码是watch的一种简单的用法: <input type="text" v-model=&quo ...
- vue中watch的详细用法(转载)
在vue中,使用watch来响应数据的变化.watch的用法大致有三种.下面代码是watch的一种简单的用法: <input type="text" v-model=&quo ...
- vue中router-link的详细用法
官网文档地址:https://router.vuejs.org/zh/api/#to 今天项目突然有需求,让vue中的一个页面跳转到另一个页面 // 字符串 <router-link to=&q ...
- 在Vue中使用TypeScript
TypeScript基础学习 初始化TS项目 Vue装饰器 vue-typescript-admin框架 1.TypeScript基础学习 2.初始化TS项目 3.Vue装饰器 Vue装饰器常用的有下 ...
- TypeScript基础以及在Vue中的应用
TypeScript推出已经很长时间了,在Angular项目中开发比较普遍,随着Vue 3.0的即将推出,TypeScript在Vue项目中使用也即将成为很大的趋势,笔者也是最近才开始研究如何在Vue ...
随机推荐
- FastAPI 学习之路(四十二)定制返回Response
我们想要在接口中返回xml格式的内容,我们应该如何实现呢. from fastapi import FastAPI,Response @app.get("/legacy/") de ...
- UltraSoft - Alpha - 测试报告
遇到的bug bug:在vue.config.js里配置proxy,并修改请求的url后仍无法连接到后端. 解决: url最后忘了'/',导致和后端不匹配,会有404.500等错误. 后端服务未打开或 ...
- find&正则表达式
标准的正则表示式格式 常用元字符 代码 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ ...
- Mac上安装Grafana
Mac上安装Grafana 一.背景 二.安装步骤 1.通过 Home Brew 安装 2.通过二进制包进行安装 1.下载 2.grafana配置文件的路径 3.修改grafana配置 1.修改默认的 ...
- proto3语法记录
protobuf 是谷歌的语言无关,平台无关,可扩展的,高效的结构化数据序列化机制,比xml和json的序列化的速度更快,此处记录一下 proto3 的语法,防止以后忘记. 注意:proto3 语法需 ...
- mongodb的简单查询
此篇文章简单的记录一下mongodb 的简单查询操作. 一.数据准备: db.persons.insertMany([ {'userId':1,name:'张三','age':20,'scores': ...
- Java RMI学习与解读(二)
Java RMI学习与解读(二) 写在前面 接上篇文章,这篇主要是跟着看下整个RMI过程中的源码并对其做简单的分析 RMI源码分析 还是先回顾下RMI流程: 创建远程对象接口(RemoteInterf ...
- EasyX安装教程
Easyx是什么 就是一款可以在Windows里让你的C++程序里显示图片等的工具. 注意:EasyX不支持Linux.MacOS.不过还有Qt等可以选择. 安装VC/VS Easyx只支持Visua ...
- hdu 5170 GTY's math problem(水,,数学,,)
题意: 给a,b,c,d. 比较a^b和c^d的大小 思路: 比较log(a^b)和log(c^d)的大小 代码: int a,b,c,d; int main(){ while(scanf(" ...
- Cesium实现右键框选
思路 1.先取消地图的右键事件 2.右键框选事件,屏幕坐标转为经纬度坐标 取消地图的右键事件 //此处容易犯一个错误:以为右键事件绑定了缩放功能,伪代码即 Cesium.MouseEvent.右键事件 ...