Vue+TypeScript+TSX

6 篇文章 0 订阅
import {Component, Prop, Vue} from 'vue-property-decorator'
import OtherComponent from '@/components/OtherComponent'

@Component({
	components: {OtherComponent}
})
export default class Exapmle extends Vue {
	// data
	public data1? string;
	public data2 number = 1;
	
	// props
	@Prop({required: true}) public prop1!: string;
	@Prop() public prop2: number;
	
	// computed
	public get myData(){ // get
		return this.data1 + '\t' + this.data2;
	}
	public set myData(v: string){ //set
		let v_arr = v.split('\t');
		this.data1 = v_arr[0];
		this.data2 = parseInt(v_arr[2]);
	}
	
	// watch
	@Watch('data1')
	protected data1Watch(newV: string, oldV: string){
		console.log(oldV, '=>', newV);
	}
	
	// 普通方法
	public buttonClickHandle(){
		this.myData= ''
	}
	
	
	// 创建时钩子函数
	created(){
		this.data1 = 'button';
		console.log('created');
	}
	
	// 挂载时钩子函数
	mounted(){
		console.log('mounted');
	}
	
	render(){
		return (
			<div>
			/* 在tsx中.sync/.stop这样的修饰符将用 '_' 来代替 '.' 监听多个_stop_sync*/
				<button	v-on-click_stop={this.buttonClickHandle} >
					{this.myData} /* 双向绑定的 */
				</button>
				// 创建默认的slot
				{this.$scopedSlots.default}
				<OtherComponent
					on={{
						['my-event1']: (param1: any, e:Event) => console.log(param1, e),
						['my-event2']: (e:Event) => console.log(e)
					}} /* 事件监听的另一种方式 */
					attr1={this.data1} /*双向绑定的*/ 
				/>
			</div>
		)
	}
}
Vue.jsTypeScript 是现代前端开发中的热门组合,它们可以一起提供高效、强类型和可维护的开发体验。以下是一般的 Vue + TypeScript 项目搭建步骤: 1. **安装 Node.js**:确保你已经安装了 Node.js,因为 Vue CLI 需要它来创建项目。 2. **安装 Vue CLI**:打开终端或命令提示符,运行 `npm install -g @vue/cli` 或者 `yarn global add @vue/cli` 来全局安装 Vue CLI。 3. **创建新项目**:使用 Vue CLI 创建一个新的 TypeScript 项目,运行 `vue create my-project --type=typescript`,选择 TypeScript 作为模板类型。 4. **选择预设**:选择合适的预设,比如 "default" 或者 "e2e",这将决定项目的结构和配置。默认预设就足够了,但如果你需要更全面的配置,可以选择其他预设。 5. **配置 Vue.config.js**:在项目根目录下的 `vue.config.js` 文件中,添加 TypeScript 相关配置,如 `transpileDependencies` 配置,确保外部库也使用 TypeScript。 6. **安装依赖**:进入项目目录后,运行 `cd my-project`,然后安装项目依赖 `npm install` 或者 `yarn`。 7. **启用 TypeScript**:在 `src` 目录下,修改每个组件、服务、测试等文件的扩展名(如 .vue -> .ts 或者 .vue -> .tsx),并启用 TypeScript 语法。 8. **编译器配置**:在 `tsconfig.json` 文件中,设置编译选项,如目标版本、模块系统等。 9. **运行项目**:使用 `npm run serve` 或者 `yarn serve` 启动项目,并查看是否成功加载 TypeScript 支持。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值