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>

页面效果:

相关推荐
Coder_Boy_3 分钟前
基于SpringAI的在线考试系统-智能考试系统-学习分析模块
java·开发语言·数据库·spring boot·ddd·tdd
2401_8948281216 分钟前
从原理到实战:随机森林算法全解析(附 Python 完整代码)
开发语言·python·算法·随机森林
玄同76517 分钟前
Python「焚诀」:吞噬所有语法糖的终极修炼手册
开发语言·数据库·人工智能·python·postgresql·自然语言处理·nlp
羽翼.玫瑰19 分钟前
关于重装Python失败(本质是未彻底卸载Python)的问题解决方案综述
开发语言·python
Amumu1213826 分钟前
Vue核心(三)
前端·javascript·vue.js
CoCo的编程之路29 分钟前
2026 前端效能革命:如何利用智能助手实现“光速”页面构建?深度横评
前端·人工智能·ai编程·comate·智能编程助手·文心快码baiducomate
RFCEO1 小时前
HTML编程 课程五、:HTML5 新增语义化标签
前端·html·html5·跨平台·语义化标签·可生成安卓/ios·html最新版本
CRMEB系统商城1 小时前
CRMEB多商户系统(PHP)- 移动端二开之基本容器组件使用
运维·开发语言·小程序·php
淮北4941 小时前
科研绘图工具R语言
开发语言·r语言
Irene19911 小时前
JavaScript中,为什么需要手动清理事件
javascript·手动清理事件监听器