vue3的computed

main
roger_home_pc 2 years ago
parent 3133f09bd2
commit 41cacb3eaf
  1. 13
      20_脚手架/vue3_test/7.src_Vue3的computed/App.vue
  2. BIN
      20_脚手架/vue3_test/7.src_Vue3的computed/assets/logo.png
  3. 50
      20_脚手架/vue3_test/7.src_Vue3的computed/components/demo.vue
  4. 10
      20_脚手架/vue3_test/7.src_Vue3的computed/main.js
  5. 15
      20_脚手架/vue3_test/src/App.vue
  6. 50
      20_脚手架/vue3_test/src/components/demo.vue

@ -0,0 +1,13 @@
<template>
<Demo/>
</template>
<script>
import {reactive} from "vue";
import Demo from "@/components/demo.vue";
export default {
name: 'App',
components: {Demo},
}
</script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,50 @@
<template>
<h1>个人信息</h1>
<input type="text" v-model="person.firstName">
<br>
<input type="text" v-model="person.lastName">
<br>
<span>全名 {{person.fullName}}</span>
<br>
全名<input type="text" v-model="person.fullName">
</template>
<script>
import {reactive, computed} from "vue";
export default {
name: 'Demo',
// computed:{
// fullName() {
// return this.person.firstName+'-'+this.person.lastName
// }
// },
setup() {
let person = reactive({
firstName: '张',
lastName: '三'
})
// -
person.fullName = computed(()=>{
return person.firstName + '-' + person.lastName
})
// -
person.fullName = computed({
get() {
return person.firstName + '-' + person.lastName
},
set(val) {
const nameArr = val.split('-')
person.firstName = nameArr[0]
person.lastName = nameArr[1]
}
})
return {
person,
}
}
}
</script>

@ -0,0 +1,10 @@
// 引入的不再是Vue构造函数, 引入的是一个名为createApp的工厂函数
import { createApp } from 'vue'
import App from './App.vue'
// createApp(App).mount('#app')
// 创建应用实例对象
const app = createApp(App)
console.log('@@@',app)
app.mount('#app')

@ -1,9 +1,5 @@
<template>
<Demo @hello="showHelloMsg" msg="你好" user="Roger">
<template v-slot:slot1>
<span>Roger</span>
</template>
</Demo>
<Demo/>
</template>
<script>
@ -13,14 +9,5 @@ import Demo from "@/components/demo.vue";
export default {
name: 'App',
components: {Demo},
setup(){
function showHelloMsg(value) {
alert(`showHelloMsg:${value}`)
}
return {
showHelloMsg
}
}
}
</script>

@ -1,35 +1,49 @@
<template>
<h1>个人信息</h1>
<h2>姓名{{ person.name }}</h2>
<h2>年龄{{ person.age }}</h2>
<button @click="test">触发showHelloMsg</button>
<input type="text" v-model="person.firstName">
<br>
<input type="text" v-model="person.lastName">
<br>
<span>全名 {{person.fullName}}</span>
<br>
全名<input type="text" v-model="person.fullName">
</template>
<script>
import {reactive} from "vue";
import {reactive, computed} from "vue";
export default {
name: 'Demo',
beforeCreate() {
console.log('---beforeCreate---')
},
props: ['msg', 'user'],
emits: ['hello'],
setup(props, context) {
console.log('---setup---', context) // $attrs $emits $slots
console.log('---setup---', context.slots) // $attrs $emits $slots
// computed:{
// fullName() {
// return this.person.firstName+'-'+this.person.lastName
// }
// },
setup() {
let person = reactive({
name: '张三',
age: 18
firstName: '张',
lastName: '三'
})
// -
person.fullName = computed(()=>{
return person.firstName + '-' + person.lastName
})
function test() {
context.emit('hello',666)
}
// -
person.fullName = computed({
get() {
return person.firstName + '-' + person.lastName
},
set(val) {
const nameArr = val.split('-')
person.firstName = nameArr[0]
person.lastName = nameArr[1]
}
})
return {
person,
test
}
}
}

Loading…
Cancel
Save