【问题标题】:Interact with component in component在组件中与组件交互
【发布时间】:2016-04-20 10:47:32
【问题描述】:

我有一个按钮组件,我在自己的组件中使用它。到目前为止工作正常。

此按钮具有布尔属性“正在加载”,用于确定是否显示微调器。我现在的问题是,如何从我的方法链接到这个按钮组件并将loading的属性更改为true?

<template>
  <div>
    <button-component></button-component>
  </div>
</template>

<script>
  import ButtonComponent from 'ButtonComponent';

  export default {
    components: {
      ButtonComponent
    },
    methods: {
      buttonClick() {
        // Set loading to true
      }
    }
</script>

【问题讨论】:

    标签: jquery vue.js


    【解决方案1】:

    有几种方法可以做到,我认为最简单的方法是使用v-ref

    &lt;button-component v-ref:loading-button&gt;&lt;/button-component&gt;

        methods: {
          buttonClick() {
            this.$refs.loadingButton.loading = true;
          }
        }
    

    或者您可以将加载属性添加到父级并同步它:

    &lt;button-component :loading.sync="loading"&gt;&lt;/button-component&gt;

        data:{
            loading: false,
        },
        methods: {
          buttonClick() {
            this.loading = true;
          }
        }
    

    【讨论】:

    • 谢谢。 v-ref 是我正在寻找的解决方案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2017-07-18
    • 1970-01-01
    • 2019-07-25
    • 2020-11-23
    • 2016-10-28
    相关资源
    最近更新 更多