vue2 export default写法,computed、methods的使用

html 复制代码
<template>
	<div>
		<h2>{{nameAll}}</h2>
		<h2>{{method}}</h2>
		<h2>{{tt()}}</h2>
		<h2>{{firstName}}</h2>
		<h2>更新后赋值数据:{{lastName}}</h2>
		<h2>赋值数据:{{writeValue}}</h2>
		<button @click="tt">vue2数据更新</button>
	</div>
</template>

<script lang ='ts' name='VueTwo'>
	export default{
		data(){
			return {
				firstName:"wu",
				lastName:"liuqi"
			}
		},
		computed:{
			nameAll:function(){
				return this.firstName + this.lastName
			},
			method(){
				return 111
			},
			writeValue:{
				get:function(){//也可以写为get(){} 页面显示数据会调用这个方法
					return this.firstName + this.lastName
				},
				set:function(value){//也可以写为set(value){}  赋值数据会调用这个方法
					this.lastName = value
					return value
				}
			}
		},
		methods:{
			tt(){
				this.writeValue = "alilailail"
			}
		}
	}
</script>
<style>
</style>

页面效果:


下面是vue3 computed函数的代码示例:

html 复制代码
<template>
	<div class="ttt">
		<button @click="updateReactive2">更新数据</button>
		<h2>{{fullName}}</h2>
		<h2>{{name3}}</h2>
	</div>
</template>

<script setup lang="ts" name="testName">
	import {ref} from 'vue'
	import {reactive} from 'vue'
	import {toRefs} from 'vue'
	import {toRef} from 'vue'
	import {computed} from 'vue'

	function updateReactive2(){
		fullName.value = "啦啦啦啦啦"
	}

	let name3 = ref('李莉莉')
	let fullName = computed({
		get(){
			return name3
		},
		set(value){
			console.log("赋值方法")
			name3.value = value
		}
	})
</script>
<style>
	.ttt{
		color:red
	}
</style>

页面效果:

相关推荐
Mike_jia4 分钟前
Keep深度解析:开源AIOps告警中枢的实践革命
前端
Mintopia9 分钟前
计算机图形学进阶探索与实践
前端·javascript·计算机图形学
代码小学僧11 分钟前
团队协作必备!pnpm 版本管理与 corepack 使用指南
前端·node.js·团队管理
一天睡25小时11 分钟前
前端工程化&&Webpack 和 Vite 的区别
前端·前端框架
gxn_mmf11 分钟前
页面需要重加载才能显示的问题修改
前端·bug
北京_宏哥12 分钟前
🔥Jmeter(二十五) - 从入门到精通 - JMeter函数 - 下篇(详解教程)
前端·jmeter·面试
天生我材必有用_吴用14 分钟前
鸿蒙开发入门到进阶:从布局基础到组件实战
前端·harmonyos·arkts
zhangxiao15 分钟前
自定义指令 - 去除所有空格和换行
前端