当前位置: 首页 > news >正文

自助网站建设系统站长素材

自助网站建设系统,站长素材,做渐变色的网站,百合seo培训在vue3中父组件访问子组件中的属性和方法是需要借助于ref: 1.<script setup> 中定义响应式变量 例如&#xff1a; const demo1 ref(null) 2.在引入的子组件标签上绑定ref属性的值与定义的响应式变量同名( <demo1 ref"demo1"/>)。 父组件代码如下&…

在vue3中父组件访问子组件中的属性和方法是需要借助于ref:

1.<script setup> 中定义响应式变量 例如: const demo1 = ref(null)

2.在引入的子组件标签上绑定ref属性的值与定义的响应式变量同名( <demo1 ref="demo1"/>)。

父组件代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<template>

  <demo1 ref="demo1"/>

  <demo2 ref="demo2"/>

  <demo3 ref="demo3"/>

</template>

<script setup>

import Demo1 from '@/components/demo1.vue'

import Demo2 from '@/components/demo2.vue'

import Demo3 from '@/components/demo3.vue'

import {ref,onMounted} from 'vue'

const demo1 = ref(null)

const demo2 = ref(null)

const demo3 = ref(null)

onMounted(()=> {

  console.log(demo1.value.count,'demo1子组件')

  console.log(demo2.value?.a,'demo2子组件')

  console.log(demo3.value.list[0],"demo3子组件")

})

</script>

子组件代码如下:

demo1.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<template>

  <h1>i'm demo1 content{{count}}</h1>

</template>

<script >

import {ref} from 'vue'

export default {

    setup () {

        const count = ref(999)

        return {

            count

        }

    }

}

此时父组件可以获取到子组件的count属性(此时子组件用的是  export default 的写法

demo2

1

2

3

4

5

6

7

8

<template>

  <h1>我是demo2</h1>

</template>

<script setup>

import {defineExpose,ref} from 'vue'

const a = ref('helloss')

</script>

当使用 <script setup> 写法会导致父组件无法访问到子组件中的属性和方法。

使用 <script setup> 的组件,想要让父组件访问到它的属性和方法需要借助与defineExpose来指定需要暴露给父组件的属性。

更改后的demo2组件

1

2

3

4

5

6

7

8

9

10

11

<template>

  <h1>我是demo2</h1>

</template>

<script setup>

import {defineExpose,ref} from 'vue'

const a = ref('helloss')

defineExpose({

    a

})

</script>

demo3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<template>

  <h1>我是demo3</h1>

</template>

<script>

export default {

  data () {

    return {

        list:['a','b','c']

    }

  },

  methods: {

    btn () {

    }

  }

}

这种方式,父组件可以正常获取到里面的属性和方法。

http://www.r43.cn/news/499.html

相关文章:

  • 网站开发人员的职责营销软文范例大全300字
  • 广州做企业网站找哪家公司好大一html网页制作作业
  • 网站展示型推广有哪些网络服务平台
  • wordpress style.宁波营销型网站建设优化建站
  • 简单的网站设计图可以打广告的平台
  • 网站的服务费账怎么做引流黑科技app
  • 什么网站可以做邮件列表网盘网页版
  • 网站开发测试网站维护中
  • 新公司刚成立做网站郑州seo哪家好
  • 永康医院网站建设优化设计单元测试卷
  • 哪家专门做特卖的网站品牌营销的四大策略
  • 做阿里巴巴网站运营腾讯广告投放平台
  • 网站做乘法表seo网站推广经理
  • 新闻门户网站建设杭州百度推广代理商
  • 公司网站开发需要多少钱搜索关键词
  • 做网站三大主流框架广州网站seo地址
  • 成都建站价格快速排名程序
  • 未被列入失信被执行人名单查询石家庄seo外包公司
  • 做物流网站有哪些内容百度提交网址入口
  • 专业网站建设公司 1861web万网官网首页
  • 美容医疗手机网站模板2345王牌浏览器
  • 青岛做视频的网站设计2024年将爆发新瘟疫
  • 做我网站百度提交入口的注意事项
  • 代办经营网站备案网站统计分析工具
  • 电商网站设计推荐亿企邦公司网站模版
  • 整站优化和单词网站推广具体内容
  • dw做的网站不显示2345网址导航电脑版
  • 网站后台这么做视频教程全球搜索引擎排行榜
  • 有没有免费做任务赚钱的网站种子搜索器
  • 外贸wordpress建站百度热议排名软件