使用TypeScript开发Uni-App项目

48 篇文章 1 订阅
22 篇文章 7 订阅

使用TypeScript开发Uni-App项目

通过HbuiderX创建uni-app项目

在新项目的vue文件中使用内联ts

<script lang="ts">

按需引入vue装饰器

import { Component,Vue ,Watch} from "vue-property-decorator";

不管干啥先把下面这句话加上。

@Component({})  //必须

常见装饰器的使用

export default class Idnex extends Vue{
		private title:String  = 'myTitle'; //响应式属性
		private num:Number = 123;           //对标之前的data函数返回的对象
		get age():Number{                   //计算属性
			return this.num;
		}
		onLoad(){
			this.printTitle();
			let a:string = '123'; 
		}
		@Watch('title')                    //watch,此处是监听title的变化
		titleChange(newVal:Number,oldVal:Number){
			console.log(newVal,oldVal);
		}
		printTitle():void{                //methods
			console.log('hahahhhaha')
		}
		
	}

简单的Demo页面

<template>
	<view class="content" @click.self="printTitle">
		<image class="logo" src="/static/logo.png" @click.stop="title = 'ggg'"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
			<view>{{age}}</view>
		</view>
	</view>
</template>

<script lang="ts">
	import { Component,Vue ,Watch} from "vue-property-decorator";
	@Component({})
	export default class Idnex extends Vue{
		private title:String  = 'myTitle'; //响应式属性
		private num:Number = 123;           //对标之前的data函数返回的对象
		get age():Number{                   //计算属性
			return this.num;
		}
		onLoad(){
			this.printTitle();
			let a:string = '123'; 
		}
		@Watch('title')                    //watch,此处是监听title的变化
		titleChange(newVal:Number,oldVal:Number){
			console.log(newVal,oldVal);
		}
		printTitle():void{                //methods
			console.log('hahahhhaha')
		}
		
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

几个注意问题

  1. 首先要在Hbuiderx里安装有关typeScript的几个插件,具体就两个一次性都装上。
  2. 如果是直接在HbuiderX里面开发编辑无需再额外安装Vue,vue-property-decorator等
  3. 如果是通过VSCode等编辑器则需要额外安装vue-property-decorator和vue-class-decorator
  4. @Component({})是必须要的,如果缺失在H5 端不会产生什么问题但是如果编译为微信小程序则会报错’mp-weixin’未定义,其它端未做验证。
  5. 看了一些介绍和Demo也可以通过vue-cli和uni-app结合的方式使用ts开发uni-app项目。有兴趣的可以去捣鼓捣鼓。
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mingyong.g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值