<template>
<div id="app">
<!-- 案例一 -->
<div class="case1">
<h1>案例一</h1>
<div v-if="loading">
<p>异步加载数据正在进行中...</p>
</div>
<div v-if="!loading">
<p>异步加载数据完成!</p>
<ul v-for="(value, index) of userList" :key="index">
<li>{{ value }}</li>
</ul>
</div>
</div>
<!-- 案例二 -->
<div class="case2">
<h1>案例二</h1>
<p>通过this.$refs获取数据</p>
<div ref="box">Hello world!</div>
</div>
<!-- 案例三 -->
<div class="case3">
<h1>案例三</h1>
<p>过滤器,拆分字符串,日期格式</p>
<p>{{ message | mySplit }}</p>
<p>{{ date | dateFormate }}</p>
</div>
</div>
</template>
<script>
export default {
// 1、初始化数据
created() {
console.log("这是created函数的内容");
this.getUserList();
this.loading = false;
},
// 2、模板加载
mounted() {
console.log("这是mounted函数的内容");
// 案例二:通过this.$refs获取数据
// 获取真实DOM
// let box = document.querySelector(".case2 div");
// let style = window.getComputedStyle(box);
// console.log(style.height);
// 获取虚拟DOM
let box = this.$refs.box;
let style = window.getComputedStyle(box);
console.log(style.height);
},
data() {
return {
message: "Hello world",
date: "2022-3-3",
userList: [],
loading: true,
};
},
methods: {
getUserList() {
// 案例一
// 3、通过计时器,模拟一个ajax异步加载数据
setTimeout(() => {
this.userList = ["jasmine", "jamsine_qiqi", "qiqi"];
}, 3000);
},
},
// 案例三:过滤器,拆分字符串,日期格式
filters: {
mySplit(value) {
return value.split("").join();
},
dateFormate(value) {
let data = new Date(value);
let year = data.getFullYear();
let month = data.getUTCMonth() + 1;
let day = data.getDate();
return `${year}年${month}月${day}日`;
},
},
};
</script>
<style>
#app {
display: flex;
justify-content: space-around;
}
.case2 div {
display: block;
width: 100px;
height: 100px;
background-color: antiquewhite;
}
</style>