<script setup>
import { ref, reactive } from 'vue'
let count = ref(1)
let arr = ref([1, 2, 3, 4, 5])
const obj2 = ref({ sex: '男' })
const state = reactive({ name: 'zhangsan ', obj: { age: 5 } })
function add() {
count.value++
arr.value.push('pink')
state.name = '哈哈哈哈'
}
</script>
<script setup>
import { reactive } from 'vue'
const state = reactive({ count: 0, msg: 'create Hello' })
function changeCount() {
state.count++
state.msg = 'changeMsg'
}
</script> -->
<template>
<!-- 只读的计算属性 -->
<input type="text" v-model.number="a">
<input type="text" v-model.number="b">
<p>{{ sum }}</p>
<hr>
<!-- 可读可写的计算属性 -->
<nav>单价:20元</nav>
数量:<input type="text" v-model.number="num">
总价:<input type="text" v-model.number="totalPrice">
<hr>
<!-- 第一种方式 -->
<!-- <button @click="obj.name = 'lisy'">点击</button> -->
<!-- 第二种方式 -->
<button @click="fn">点击</button>
</template>
<script setup>
//FIXME:按需 最终打包大小 tree shaking
import { ref, computed, watch, reactive, watchEffect } from 'vue'
const a = ref(0)
const b = ref(0)
const sum = computed(() => {
// 只读
return a.value + b.value
})
// 监听单个属性(基本数据类型)
watch(a, (newValue, oldValue) => { console.log('新值', newValue, '旧值', oldValue) })
// 监听多个属性(基本数据类型)
watch([a, b], (newValue, oldValue) => { console.log('新值', newValue, '旧值', oldValue) })
const price = 20 //单价
const num = ref(0) // 数量
const totalPrice = computed({
// 可读可写
get() {
return num.value * price
},
set(newValue) {
num.value = newValue / price
}
})
// 第一种方式:配合ref,但是要加value (复杂数据类型)
// const obj = ref({ name: 'zhangsan', age: 20 })
// watch(() => obj.value.name, (newValue, oldValue) => {
// console.log('新值', newValue, '旧值', oldValue)
// }, { immediate: true }, { deep: true })
// 第二种方式:配合reactive (复杂数据类型)
const obj = reactive({ name: 'zhangsan', age: 20 })
watch(() => obj.name, (newValue, oldValue) => {
console.log('新值', newValue, '旧值', oldValue)
})
function fn() {
obj.name = 'list11'
}
//TODO: 一开始就执行,可以监听对象(依赖的变量特别多的话,并且要立即执行的话就是用这个)
// watchEffect(() => {
// console.log(a.value + b.value);
// console.log(obj);//Proxy {name: 'zhangsan', age: 20}
// })
// FIXME:监视整个对象
// watch(obj,(newValue) =>{
// console.log(123);
// })
</script>
<template>
<h1 ref="abc">hello</h1>
<el-table :data="list" border stripe>
<el-table-column type="index" width="80" align="center" />
<el-table-column label="书名" prop="bookname" align="center" />
<el-table-column label="作者" prop="author" align="center" />
<el-table-column label="出版社" prop="publisher" align="center" />
<el-table-column label="时间" prop="createdAt" align="center" />
</el-table>
</template>
<script setup>
import axios from 'axios'
import { onMounted, onUnmounted, ref } from 'vue'
const abc = ref(null)
const list = ref([])
onMounted(async () => {
const { data: { data } } = await axios({
url: 'http://ajax-api.itheima.net/api/books',
})
list.value = data
})
</script>