• uniApp组件如何使用笔记


    什么是组件

     对我感觉组件就是一个可以复用的Vue实例,例如在一个页面有两个复用的添加按钮

    如何注册组件?

    1.在components中新建一个father.vue

    1. <template name="father">
    2. <view>
    3. view>
    4. template>
    5. <script>
    6. export default {
    7. name:'father',
    8. props:{
    9. },
    10. data() {
    11. return {
    12. }
    13. },
    14. methods: {
    15. }
    16. }
    17. script>
    18. <style>
    19. style>

    2.在main.js中引用

    1. import father from '@/components/father.vue'
    2. Vue.component('father',father)

     3.在子页面引用即可

    1. <template >
    2. <view>
    3. <father :list="treeList">father>
    4. <father :list="treeList">father>
    5. view>
    6. template>

    假设在A页面接受到的数据通过子组件遍历出来,如何在子组件的父组件接收到数据?

    A界面样式

    1. <template >
    2. <view>
    3. <father :list="treeList">father>
    4. view>
    5. template>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. treeList:[
    11. {
    12. id:'1',
    13. name:'曲坊苗圃',
    14. array:[{
    15. name:'向日葵',
    16. num:1,
    17. id:33
    18. },
    19. {
    20. name:'洋槐花',
    21. num:3,
    22. id:44
    23. }
    24. ]
    25. },
    26. {
    27. id:'2',
    28. name:'能源苗圃',
    29. array:[
    30. ]
    31. }
    32. ],
    33. }
    34. },
    35. methods: {
    36. }
    37. }
    38. script>
    39. <style>
    40. style>

    b界面接受并且遍历出来

    immediate如果为true 代表如果在 wacth 里声明了之后,就会立即先去执行里面的handler方法

    1. <template name="father">
    2. <view>
    3. <view style="margin-top: 50px;" v-for="(item,index) in treeArray">
    4. <view class="">
    5. <view class="">
    6. <view class="" @click="pointtitle(item)">
    7. {{item.name}}
    8. view>
    9. <view class="" v-for="(sonItem,sonIndex) in item.array" style="padding-left: 15px;">
    10. {{sonItem.name}}
    11. view>
    12. view>
    13. view>
    14. view>
    15. view>
    16. template>
    17. <script>
    18. export default {
    19. name:'father',
    20. props:{
    21. list:[Array]
    22. },
    23. data() {
    24. return {
    25. treeArray:[],
    26. number:5
    27. }
    28. },
    29. watch:{
    30. list:{
    31. immediate: true,
    32. handler(newValue,oldValue){
    33. this.treeArray = newValue
    34. }
    35. }
    36. },
    37. methods: {
    38. /* 点击标题 */
    39. pointtitle(Item){
    40. for (var i = 0; i < this.treeArray.length; i++) {
    41. if(Item.id==this.treeArray[i].id){
    42. if(this.treeArray[i].array.length==0){
    43. console.log('当前苗圃下没有苗木')
    44. }else{
    45. console.log('当前下面有')
    46. }
    47. }
    48. }
    49. },
    50. }
    51. }
    52. script>
    53. <style>
    54. style>

    假设点击子组件的时候怎么将数据传给父组件?

    1.谁是父组件谁是子组件,总结就是谁包含谁谁就是父组件,这句话的意思是A界面有个组件B当点击B的某一项后会在A界面输出出来。

    通过$emit 父子组件之间的通信

    在B假面写pointemit()这个函数,别的不看

    1. <template name="father">
    2. <view>
    3. <view style="margin-top: 50px;" v-for="(item,index) in treeArray">
    4. <view class="">
    5. <view class="">
    6. <view class="" @click="pointtitle(item)">
    7. {{item.name}}
    8. view>
    9. <view class="" v-for="(sonItem,sonIndex) in item.array" style="padding-left: 15px;" @click="pointemit(sonItem)">
    10. {{sonItem.name}}
    11. view>
    12. view>
    13. view>
    14. view>
    15. view>
    16. template>
    17. <script>
    18. export default {
    19. name:'father',
    20. props:{
    21. list:[Array]
    22. },
    23. data() {
    24. return {
    25. treeArray:[],
    26. number:5
    27. }
    28. },
    29. watch:{
    30. list:{
    31. immediate: true,
    32. handler(newValue,oldValue){
    33. this.treeArray = newValue
    34. }
    35. }
    36. },
    37. methods: {
    38. /* 点击标题 */
    39. pointtitle(Item){
    40. for (var i = 0; i < this.treeArray.length; i++) {
    41. if(Item.id==this.treeArray[i].id){
    42. if(this.treeArray[i].array.length==0){
    43. console.log('当前苗圃下没有苗木')
    44. }else{
    45. console.log('当前下面有')
    46. }
    47. }
    48. }
    49. },
    50. // 把子组件值传递给父组件
    51. pointemit(item){
    52. this.$emit('treeItem', item)
    53. }
    54. }
    55. }
    56. script>
    57. <style>
    58. style>

    在A界面接受

    1. <template >
    2. <view>
    3. <father :list="treeList" @treeItem="treeItem">father>
    4. view>
    5. template>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. treeList:[
    11. {
    12. id:'1',
    13. name:'曲坊苗圃',
    14. array:[{
    15. name:'向日葵',
    16. num:1,
    17. id:33
    18. },
    19. {
    20. name:'洋槐花',
    21. num:3,
    22. id:44
    23. }
    24. ]
    25. },
    26. {
    27. id:'2',
    28. name:'能源苗圃',
    29. array:[
    30. ]
    31. }
    32. ],
    33. }
    34. },
    35. methods: {
    36. treeItem(item){
    37. console.log(item)
    38. }
    39. }
    40. }
    41. script>
    42. <style>
    43. style>

    一般组件搭配这插槽使用(之前写过放这里了)

    (141条消息) vue插槽solt ,uni.app_liuwenqing11的博客-CSDN博客

  • 相关阅读:
    《Kubernetes部署篇:Ubuntu20.04基于外部etcd+部署kubernetes1.25.14集群(多主多从)》
    https跳过SSL认证时是不是就是不加密的,相当于http?
    react基本使用
    PIC单片机1——按钮测试
    LeetCode209长度最小子数组
    MATLAB算法实战应用案例精讲-【图像处理】目标检测(补充篇)(附实战案例及代码实现)
    跨平台开发方案调研
    智能家居和物联网技术
    中介者设计模式
    前端 JS 经典:宏任务、微任务、事件循环(EventLoop)
  • 原文地址:https://blog.csdn.net/liuwenqing11/article/details/125899787