setup执行的时机:
setup的两个参数:
this.$attrs
this.$slots
this.$emit
两个参数
setup(props, context) {
console.log("--setup--", props);
console.log("--setup--", context);
}
<template>
<div id="app">
<h1>我是Vue2写的效果h1>
<Demo @hello="showHelloMsg" msg="信息" school="TYUT">
<template v-slot:qwe>
<span>具名插槽一span>
template>
Demo>
div>
template>
<script>
import Demo from "./components/Demo.vue";
export default {
name: "App",
components: { Demo },
setup() {
function showHelloMsg(value) {
alert(`你好啊,我出发了hello事件,我收到的参数是${value}`);
}
return {
showHelloMsg,
};
},
};
script>
<template>
<div>
<h1>一个人的信息h1>
<button @click="test">测试触发一下Demo组件的Hello事件button>
div>
template>
<script>
import { reactive } from "vue";
export default {
name: "Demo",
props: ["msg"],
emits: ["hello"],
setup(props, context) {
console.log("--setup--", props);
console.log("--setup--", context); // attrs,emit,slots
// 数据
let person = reactive({
name: "张三",
age: 18,
});
// 方法
function test() {
context.emit("hello", 777);
}
return {
person,
test,
};
},
};
script>
import {reactive,computed} from 'vue'
export default {
name: 'Demo',
setup() {
let person = reactive({
fN: '张',
lN: '三'
})
// 计算属性:没有考虑计算属性被修改的情况
person.fullName = computed(() => {
return person.firstName + '-' + person.lastName
})
// 计算属性-完整写法
person.fullName = computed({
get() {
return person.fN + '_' + person.lN
},
set(value) {
const nameArr = value.split('_')
person.fN = nameArr[0]
person.fN = nameArr[1]
}
})
}
}
两个小坑
监视reactive所定义的一个响应式数据时:
监视reactive定义的响应式数据中某个属性时:deep配置有效
<template>
<div>
<h1>当前求和为: {{ sum }}</h1>
<h1>msg:{{ msg }}</h1>
<button @click="sum++">点我+1</button>
<h2>姓名: {{ person.name }}</h2>
<h2>年龄: {{ person.age }}</h2>
<button @click="person.age++">增长年龄</button>
</div>
</template>
<script>
import { ref, watch, reactive } from "vue";
export default {
name: "Demo",
// vue2
// watch: {
// sum(oldValue, NewValue) {
// console.log("sum的值改变了,newValue,oldValue");
// },
// // sum: {
// // immediate: true,
// // deep: true,
// // handler(newValue, oldValue) {},
// // },
// },
setup() {
// 数据
let sum = ref(0);
let msg = ref("你好");
let person = reactive({
name: "张三",
age: 18,
});
// 监视
// 情况一:监视ref所定义的多个响应式数据
watch(
[sum, msg],
(newValue, oldValue) => {
console.log("sum变了", newValue, oldValue);
},
{ immediate: true }
);
// 情况二:监视reactive所定义的一个响应式数据的全部属性
// 此处无法正确获取oldValue
// 强制开启了深度监视(deep配置无效)
watch(person, (nV, oV) => {
console.log("person变化了", nV, oV);
});
// 情况三:监视reactive所定义的一个响应式数据中的某个属性
watch(
() => person.age,
(nV, oV) => {
console.log("person变化了", nV, oV);
},
{ deep: false }
);
// 情况四:监视reactive所定义的一个响应式数据中的某些属性
watch([() => person.name, () => person.age], (newValue, oldValue) => {
console.log("person的name或age变化了", newValue, oldValue);
});
// 特殊情况,监视person.job
watch(
() => person.job,
(nV, oV) => {
console.log("person的job变化了", newValue, oldValue);
},
{ deep: true } // 此处由于监视的时reactive定义的对象中的某个属性,所以deep配置有效
);
// 返回一个对象
return {
sum,
msg,
person,
};
},
};
</script>
watch(
[sum, msg],
(newValue, oldValue) => {
console.log("sum变了", newValue, oldValue);
},
{ immediate: true }
);
// 此处无法正确获取oldValue
// 强制开启了深度监视(deep配置无效)
watch(person, (nV, oV) => {
console.log("person变化了", nV, oV);
},{deep:false}); // deep配置无效
watch(
() => person.age,
(nV, oV) => {
console.log("person变化了", nV, oV);
},
{ deep: false }
);
watch([() => person.name, () => person.age], (newValue, oldValue) => {
console.log("person的name或age变化了", newValue, oldValue);
});
watchEffect(() => {
const x1 = sum.value;
const x2 = person.job;
console.log("watchEffect所指定的回调执行了");
});
<template>
<div>
<h1>当前求和为: {{ sum }}</h1>
<h1>msg:{{ msg }}</h1>
<button @click="sum++">点我+1</button>
</div>
</template>
<script>
import {
ref,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
} from "vue";
export default {
name: "Demo",
setup() {
let sum = ref(0);
let msg = ref("你好");
onBeforeMount(() => {
console.log("---beforeMount--");
});
onMounted(() => {
console.log("---mounted--");
});
onBeforeUpdate(() => {
console.log("---beforeUpdata--");
});
onUpdated(() => {
console.log("---updated--");
});
onBeforeUnmount(() => {
console.log("---beforeUpdata--");
});
onUnmounted(() => {
console.log("---updated--");
});
return {
sum,
msg,
};
},
// // 通过配置项的形式使用生命周期钩子函数
// beforeCreate() {
// console.log("---beforeCreate--");
// },
// created() {
// console.log("---created--");
// },
// beforeMount() {
// console.log("---beforeMount--");
// },
// mounted() {
// console.log("---mounted--");
// },
// beforeUpdate() {
// console.log("---beforeUpdata--");
// },
// updated() {
// console.log("---updated--");
// },
// beforeUnmount() {
// console.log("---beforeUpdata--");
// },
// unmounted() {
// console.log("---updated--");
// },
};
</script>