01、App.vue代码如下:
<template> <div class="app"> <h2>{{ title }}</h2> <button @click="showLog">点我数组子页面年龄</button> <!-- 使用了ref来获取子组件的属性--> <Person ref="person001"/> </div> </template> <script lang="ts" setup name="App"> // JS或TS import Person from './view/Person.vue' import {ref} from 'vue' let title = ref('好好学习,天天向上') // 这里是获取子组件的属性 let person001 = ref() function showLog() { title.value = person001.value.name + person001.value.age; } </script> <!--样式 scoped表示仅本单元有效--> <style scoped> .app { background-color: #ddd; box-shadow: 0 0 10px; border-radius: 10px; padding: 20px; } </style>
02、Person.vue代码如下:
<template> <div class="person"> <h1>中国</h1> <h2 ref="title2">北京</h2> <h2>{{ name }}</h2> <h2>{{ age }}</h2> <button @click="addAge()">点我增加年龄</button> </div> </template> <script lang="ts" name="Person001" setup> import {defineExpose, ref} from 'vue' let title2 = ref() let name = ref('张三') let age = ref(18) function addAge() { age.value += 1 } defineExpose({age, name}); </script> <style scoped> .person { background-color: #ddd; box-shadow: 0 0 10px; border-radius: 10px; padding: 20px; button { margin: 0 5px; } } </style>
02、效果如下:
标签:TypeScript,defineExpose,age,vue,let,10px,ref,页面 From: https://www.cnblogs.com/tianpan2019/p/18364937