1。设置的生命周期
既然我在 script 标签中编写了设置,那么我们自然也涵盖了生命周期问题。仔细阅读了官方文件后,我整理(抄)如下:
可选API | 钩入(设置) |
---|---|
创作前 | 不需要* |
创建者 | 不需要* |
组装前 | 安装前 |
已安装 | 已安装 |
更新前 | 更新前 |
更新了 | 更新 |
断开连接之前 | 在卸载之前 |
未组装 | 已卸载 |
捕获错误 | OnError 捕获者 |
渲染跟踪 | isRenderTracked |
渲染开始 | 已触发渲染 |
我们日常开发中用的最多的基本都是mounted,所以从上表我们可以看到,我们可以很方便的使用onMounted进行设置。例如:
<script setup>
import { reactive, onMounted } from "vue";
const data = reactive({
mas:"我爱祖国"
});
const init = () => {
console.log(data.msg)
};
onMounted(() => {
init()
});
</script>
啊啊,没有它我该怎么办?
由于我在项目中使用了element plus组件库,所以我熟练地打开文档并开始复制。突然我看到了几天没见过的这个.$xxx。我在哪里见过这个护士?
<script setup>
import { reactive, onMounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const data = reactive({
msg: "hello,vue3"
});
const init = () => {
proxy.$alert('这是一段内容', '标题名称', {
confirmButtonText: '确定',
callback: action => {
proxy.$message({
type: 'info',
message: `action: ${ action }`
});
}
});
};
onMounted(() => {
init()
});
</script>
经过测试,发现可以正常工作,所以这个问题就暂时结束了。
获取参数和路由跳数真的那么容易吗?
import { reactive, onMounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const data = reactive({
msg: "hello,vue3"
});
const init = () => {
if(proxy.$route.query.code){
proxy.$alert('这是一段内容', '标题名称', {
confirmButtonText: '确定',
callback: action => {
proxy.$message({
type: 'info',
message: `action: ${ action }`
});
}
});
}
};
onMounted(() => {
init()
});
import { reactive, onMounted, getCurrentInstance } from "vue";
import { useRoute, useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const data = reactive({
msg: "hello,vue3"
});
const init = () => {
// if(proxy.$route.query.code){
// proxy.$alert('这是一段内容', '标题名称', {
// confirmButtonText: '确定',
// callback: action => {
// proxy.$message({
// type: 'info',
// message: `action: ${ action }`
// });
// }
// });
// }
// };
const route = useRoute();
if(route.query.code){
proxy.$alert('这是一段内容', '标题名称', {
confirmButtonText: '确定',
callback: action => {
proxy.$message({
type: 'info',
message: `action: ${ action }`
});
}
});
}
};
onMounted(() => {
init()
});
结语
以上内容是小蔡努力的成果。不足之处欢迎大家指出。如果有任何疑问也可以评论。我会尽快回复您。转载时请注明出处。
–wy卷心菜
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。