【问题标题】:Trigger a timer from parent component in Vue 3从 Vue 3 中的父组件触发计时器
【发布时间】:2021-09-17 06:46:46
【问题描述】:

如何在 Vue 3 中从其父组件正确触发子组件中的方法。事件总线以外的东西。

定时器触发方法如下:

methods: {
    startTimer: function () {
      this.timer = setInterval(() => this.countdown(), 1000);   
    },

我需要通过按下父级中的按钮来关闭它。

【问题讨论】:

    标签: vue.js vue-component vuejs3


    【解决方案1】:

    在父组件中为子组件添加一个 ref 并使用该 ref 触发该方法:

    <Child ref="childRef" />
    

    并在某处触发它:

     this.$refs.childRef.startTimer()
    

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2020-04-16
      相关资源
      最近更新 更多