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>

页面效果:

相关推荐
Dxy12393102161 小时前
python如何做声音识别
开发语言·python
superlls1 小时前
(场景题)Java 导出 Excel 的两种方式
java·开发语言·后端·excel
艾小码1 小时前
前端别再乱存数据了!这3种存储方案让你的应用快如闪电
前端·javascript
黄毛火烧雪下1 小时前
HTML 的底层原理
前端·html
球球和皮皮1 小时前
Babylon.js学习之路《添加自定义摇杆控制相机》
javascript·3d·前端框架·babylon.js
Moment1 小时前
面经分享——字节前端一面
前端·javascript·面试
@LetsTGBot搜索引擎机器人2 小时前
Telegram 被封是什么原因?如何解决?(附 @letstgbot 搜索引擎重连技巧)
开发语言·python·搜索引擎·机器人·.net
十步杀一人_千里不留行3 小时前
Google 登录集成教程(Web + Expo 移动端)
前端
沐欣工作室_lvyiyi4 小时前
基于Matlab的简易振动信号分析系统(论文+仿真)
开发语言·matlab·毕业设计·振动信号分析
gAlAxy...6 小时前
IntelliJ IDEA 四种项目构建:从普通 Java 到 Maven Web 项目
前端·firefox