首页 > 其他分享 >Stemciljs学习2、组件生命周期

Stemciljs学习2、组件生命周期

时间:2023-04-04 17:04:51浏览次数:56  
标签:Stemciljs 生命周期 console log text Prop 组件

组件有许多生命周期方法,可用于了解组件何时“将”和“执行”加载、更新和呈现。可以将这些方法添加到组件中,以便在正确的时间挂接到操作中。

在组件类中实现以下方法之一,Stencil 将以正确的顺序自动调用它们:

单组件的生命周期

import { Component, Host, h, Prop, Watch } from '@stencil/core';

@Component({
  tag: 'web-text',
  styleUrl: 'web-text.css',
  shadow: true,
})
export class WebText {
  @Prop() text: string;

  @Watch('text')
  handlerTextWatcher(val: string, oldVal: string) {
    console.log('新值:', val, ',旧值:', oldVal);
    console.log('生命周期:watch');
  }

  connectedCallback() {
    console.log('生命周期:connectedCallback');
  }

  componentWillLoad() {
    console.log('生命周期:componentWillLoad');
  }

  componentWillRender() {
    console.log('生命周期:componentWillRender');
  }

  render() {
    console.log('生命周期:render');
    return <Host>{this.text}</Host>;
  }

  componentDidRender() {
    console.log('生命周期:componentDidRender');
  }

  componentDidLoad() {
    console.log('生命周期:componentDidLoad');
  }

  componentShouldUpdate() {
    console.log('生命周期:componentShouldUpdate');
  }

  componentWillUpdate() {
    console.log('生命周期:componentWillUpdate');
  }

  componentDidUpdate() {
    console.log('生命周期:componentDidUpdate');
  }

  disconnectedCallback() {
    console.log('生命周期:disconnectedCallback');
  }
}

未更改Prop text时console输入的结果

Stemciljs学习2、组件生命周期_Stenciljs

更改Prop text后console输出的结果

Stemciljs学习2、组件生命周期_webcomponent_02

具体的生命图示如下(源自官方网站)

Stemciljs学习2、组件生命周期_css_03

更多生命周期相关的细节请参考官方网站

如果要更改PropState的值,建议在componentWill开头的生命周期中去更改,因为带will的生命周期会在组件渲染前执行,在did生命周期内区修改PropState的值,有可能会造成死循环;如果必须在did生命周期内区修改PropState的值,请一定要做好前置判断

嵌套组件的生命周期

<component-a> 
    <component-b> 
        <component-c></component-c> 
    </component-b> 
</component-a>
  1. component-a - componentWillLoad()
  2. component-b - componentWillLoad()
  3. component-c - componentWillLoad()
  4. component-c - componentDidLoad()
  5. component-b - componentDidLoad()
  6. component-a - componentDidLoad()

结束语

在上一篇中)我们讲解了如何使用stemcil-cli 搭建组件库的方式;那么经过本篇的讲解,我们已经了解了其组件的生命周期;下一篇将讲解stenciljs的相关装饰器。



标签:Stemciljs,生命周期,console,log,text,Prop,组件
From: https://blog.51cto.com/jikun/6169045

相关文章

  • 【Spring原理分析-Bean生命周期+Spring原理分析-Bean后处理器】
    Spring原理分析-Bean生命周期一、Bean生命周期1、Bean生命周期测试2、生命周期顺序二、自定义BeanPostProcessor实现增强三、模板方法-设计模式一、Bean生命周期1、Bean生命周期测试①编写LifeCycleBean类②编写启动类③测试2、生命周期顺序执行顺序:构造方法→@Autowired→......
  • android四大组件
    Android开发的四大组件,本文主要分为一、Activity详解二、Service详解三、BroadcastReceiver详解四、ContentProvider详解外加一个重要组件intent的详解。一、Activity详解Activty的生命周期的也就是它所在进程的生命周期。 一个Activity的启动顺序:onCreate()——>onStart()——......
  • 子组件传递数据给父组件的三种方法
    1.props<template><!--App.vue--><divid="app"><!--向子组件中传入函数--><Student:receive="receive"></Student></div></template><script>importStudentfrom'./co......
  • React父组件调用子组件属性和方法
    子组件暴露自身的属性和方法父组件使用ref绑定对应的子组件。调用即可类组件绑定ref示例importReactfrom'react'importChildfrom'./Child'exportdefaultclassParentextendsReact.Component{//...render(){return(<div><Childre......
  • MyBatis的执行流程及核心组件
    MyBatis的执行流程及核心组件如图所示。基本组件介绍Configuration用于描述MyBatis的主配置信息,其他组件需要获取配置信息时,直接通过Configuration对象获取。除此之外,MyBatis在应用启动时,将Mapper配置信息、类型别名、TypeHandler等注册到Configuration组件中,其他组件需要这......
  • 生命周期与插件
                 ......
  • Glide生命周期原理
    vivo互联网技术微信公众号 作者:连凌能AndroidApp中图片的展示是很基本也很重要的一个功能,在Android平台上有很多的图片加载解决方案,但是官方认可的是Glide。AndroidApp的页面是有生命周期的,Glide比较好的一个功能就是具有生命周期管理功能,能够根据页面和APP的生命周期来管理......
  • windows 服务开发组件之Topshelf
    常用的windows服务开发有vs自带的windows服务开发,但是操作起来不是很便利,相比Topshelf后者更加的方便,且易于调试,安装,卸载.1、安装通过nuget搜索最新版本的topshelf,并安装到控制台项目(netcore和net均可)中,一般都是用控制台调试,然后通过topshelf安装成windows服务.官方......
  • 5、pod生命周期
      我们一般将pod对象从创建至终的这段时间范围称为pod的生命周期,它主要包含下面的过程:pod创建过程运行初始化容器(initcontainer)过程运行主容器(maincontainer)容器启动后钩子(poststart)、容器终止前钩子(prestop)容器的存活性探测(livenessprobe)、就绪性探测(readinessp......
  • React Native学习笔记(三)—— 组件
    一、ReactNative项目1.1、创建ReactNative项目ReactNative有一个内置的命令行界面,你可以用它来生成一个新项目。您可以使用Node.js附带的访问它,而无需全局安装任何内容。让我们创建一个名为“AwesomeProject”的新ReactNative项目:npxnpxreact-native@latestinitAw......