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

<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函数的代码示例:

<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. Vue2Vue3自定义指令写法

    2024-03-28 04:44:01       57 阅读
  2. watch监听vue2vue3写法

    2024-03-28 04:44:01       27 阅读
  3. Vue3:重构Piniastore,使用组合式写法实现

    2024-03-28 04:44:01       41 阅读
  4. Vue2-3、Vue 基本使用

    2024-03-28 04:44:01       80 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 04:44:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 04:44:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 04:44:01       87 阅读
  4. Python语言-面向对象

    2024-03-28 04:44:01       96 阅读

热门阅读

  1. 关于分布式系统设计的个人看法和经验

    2024-03-28 04:44:01       38 阅读
  2. 大历史下的 pacing:程序员视角和大历史视角

    2024-03-28 04:44:01       48 阅读
  3. Linux的相关指令总结

    2024-03-28 04:44:01       40 阅读
  4. 前端下载超大文件的完整方案

    2024-03-28 04:44:01       39 阅读
  5. 题目 2850: 输出亲朋字符串

    2024-03-28 04:44:01       39 阅读
  6. typeScript6(其他类型)

    2024-03-28 04:44:01       43 阅读
  7. git的实际应用场景

    2024-03-28 04:44:01       38 阅读
  8. Docker的常用命令

    2024-03-28 04:44:01       38 阅读