1️⃣ 组件生命周期图示

image.png
lifecycle2.png

1️⃣ 嵌套组件生命周期初次渲染的执行顺序

image.png
image.png

1️⃣ 嵌套组件更新数据的生命周期执行顺序

image.png

1️⃣ 加载远程数据

  1. export default {
  2. data(){
  3. return {
  4. news: []
  5. }
  6. },
  7. // 写法一
  8. created(){
  9. getNews().then((params) => {
  10. this.news = params;
  11. })
  12. }
  13. // 写法二
  14. async created(){
  15. this.news = await getNews();
  16. }
  17. }

1️⃣ 组件中直接操作DOM

  1. <!-- >>>>>>>>>> App.vue >>>>>>>>>> -->
  2. <template>
  3. <div id="app">
  4. <div ref="elementDomOne">elementDomOne</div>
  5. <div ref="elementDomTwo">elementDomTwo</div>
  6. <div ref="elementDomThree">elementDomThree</div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. mounted() {
  12. console.log(this.$refs);
  13. console.log(this.$refs.elementDomOne.innerText);
  14. console.log(this.$refs.elementDomTwo.innerText);
  15. console.log(this.$refs.elementDomThree.innerText);
  16. },
  17. };
  18. </script>

image.png

1️⃣ 启动和清除定时器

  1. export default {
  2. data(){
  3. return {
  4. timer: null
  5. }
  6. },
  7. created(){
  8. this.timer = setInterval(()=>{
  9. ...
  10. }, 1000)
  11. },
  12. destroyed(){
  13. clearInterval(this.timer);
  14. }
  15. }