当前位置:   article > 正文

arkTs 跨组件传输数据和组件封装_arkts 封装组件

arkts 封装组件

父页面

  1. import titleComponent from '../../components/TitleComponent'
  2. @Component
  3. export default struct Home {
  4. @State message: string = 'home页面'
  5. build() {
  6. Column({ space: 5 }) {
  7. titleComponent({
  8. title:"我是标题",
  9. content:"我是内容"
  10. })
  11. }
  12. .width('100%')
  13. .height("100%")
  14. }
  15. }

子页面

  1. @Component
  2. export default struct titleComponent{
  3. private title = "第二期"
  4. private content = "/会议信息"
  5. build() {
  6. Column(){
  7. Text(this.title)
  8. Text(this.content)
  9. }.padding(10)
  10. }
  11. }

即可解决父子组件传参问题

然后我个人组件封装思路

因为arkTs的样式编写需要一直点下去有几个样式就需要点几个如Text这组件部分,在复杂样式的情况这个链式操作会非常的长,所以为了方便日后使用,尽量官网的原生组件进行一次二次封装,如下

  1. @Component
  2. export default struct myText{
  3. private title = ""
  4. private color = ""
  5. private fontSize = "16"
  6. private fontWeight = ""
  7. private fontAlign = TextAlign.Center
  8. build() {
  9. Column(){
  10. Text(this.title)
  11. .fontColor(this.color)
  12. .fontSize(this.fontSize)
  13. .fontWeight(this.fontWeight)
  14. .textAlign(this.fontAlign)
  15. }
  16. }
  17. }

使用如下,myText就是我封装的组件,这样使用即可

myText({
  color:"#000",
  fontSize:16,
  title:"文字",
  fontWeight:600
})
  1. import myText from "../components/MyText"
  2. import myImg from "../components/MyImg"
  3. @Component
  4. export default struct titleComponent{
  5. private titleColor = "#d3292b"
  6. private textColor = "#A7A7A7"
  7. private title = "第二期"
  8. private content = "/会议信息"
  9. build() {
  10. Column(){
  11. Flex({justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
  12. Flex({justifyContent:FlexAlign.Start,alignItems:ItemAlign.Center}){
  13. myImg({
  14. src:$rawfile("img/icon.png"),
  15. imgWidth:"35",
  16. imgHeight:"35"
  17. }).margin({right:8})
  18. myText({
  19. color:this.titleColor,
  20. fontSize:20,
  21. title:this.title,
  22. fontWeight:600
  23. }).margin({right:5})
  24. myText({
  25. color:this.textColor,
  26. fontSize:16,
  27. title:this.content,
  28. fontWeight:600
  29. })
  30. }.width(230)
  31. Column(){
  32. myText({
  33. color:this.textColor,
  34. fontSize:14,
  35. title:"更多",
  36. fontWeight:600
  37. })
  38. }
  39. }
  40. }.padding(10)
  41. }
  42. }

以上是个人的思路想法,如有不足之处望体谅

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/228799
推荐阅读
相关标签
  

闽ICP备14008679号