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>

页面效果:

相关推荐
云知谷1 分钟前
【HTML】网络数据是如何渲染成HTML网页页面显示的
开发语言·网络·计算机网络·html
昔人'3 分钟前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css
昔人'5 分钟前
css `dorp-shadow`
前端·css
流***陌13 分钟前
扭蛋机 Roll 福利房小程序前端功能设计:融合趣味互动与福利适配
前端·小程序
可触的未来,发芽的智生23 分钟前
新奇特:黑猫警长的纳米世界,忆阻器与神经网络的智慧
javascript·人工智能·python·神经网络·架构
lly2024061 小时前
SQL ROUND() 函数详解
开发语言
烛阴1 小时前
用 Python 揭秘 IP 地址背后的地理位置和信息
前端·python
大宝剑1701 小时前
python环境安装
开发语言·python
前端开发爱好者1 小时前
尤雨溪官宣:"新玩具" 比 Prettier 快 45 倍!
前端·javascript·vue.js