简体   繁体   English

vue.js - 从另一个组件调用组件方法

[英]vue.js - call component method from another component

I want to use the getProjects() method of Component 2 to refresh my table after adding a data.我想在添加数据后使用组件 2 的 getProjects() 方法来刷新我的表。 Both of these components are attached in a blade.php这两个组件都附加在blade.php中

Component 1.vue组件1.vue

 methods: {
  addData() {
    this.attemptSubmit = true;
    this.validate = true;
    this.errors = [];
    if (this.errors) event.preventDefault();
    axios
    .post('/api/department', {
                department: this.department,
                section: this.section
    })
    .then(response => {
      this.validate = false;
      this.department = '';
      this.section = '';
      this.errors = '';
      this.output = response.data;
      //--- I want to insert the getProjects() method here 
    })
    .catch(error => this.errors = error.response.data.errors)
  },

Component 2.vue组件2.vue

methods: {
    getProjects(url = '/api/departments') {
      this.tableData.draw++;
      axios
      .get(url, {params: this.tableData})
      .then(response => {
        let data = response.data;
        if (this.tableData.draw == data.draw) {
          this.projects = data.data.data;
          this.configPagination(data.data);
        }
      })
      .catch(errors => {
        console.log(errors);
      });
    },

You should use vuex.你应该使用 vuex。 See https://medium.com/js-dojo/vuex-2638ba4b1d76https://medium.com/js-dojo/vuex-2638ba4b1d76

You call the vuex action from your component and in this action, you can make the axios call directly or use another service to make your api calls.您从组件调用 vuex 操作,在此操作中,您可以直接进行 axios 调用或使用其他服务进行 api 调用。

Mutations should never be called from components only from actions inside the store.绝不能仅从 store 内的操作中从组件中调用突变。

kind regards亲切的问候

yes it is possible.对的,这是可能的。 Step 3 will do what you explicitly asked but there are better ways.第 3 步将执行您明确要求的操作,但有更好的方法。

Three ways.三种方式。

1) VUEX: Move the method you wish to call, to Vuex and any associated reactive data properties to the store. 1) VUEX:将您希望调用的方法移动到 Vuex 和任何关联的响应式数据属性到 store。 And use getters to retrieve the updated data.并使用 getter 来检索更新的数据。

2) MIXINS: With mixins you would move your component2.vue and associated data properties to a mixin, and import it in both components, allowing it to be called from each. 2) MIXINS:使用 mixin,您可以将 component2.vue 和关联的数据属性移动到 mixin,并将其导入到两个组件中,允许从每个组件调用它。

3) Hacky fix that actually calls the method from another component (not best practice) 3) 实际上从另一个组件调用方法的Hacky 修复(不是最佳实践)

You can mount methods to $root and call them from another component, by emiting a event.您可以通过发出事件将方法挂载到 $root 并从另一个组件调用它们。 Using your example it would look like the following.使用您的示例,它看起来如下所示。

Components2.vue // mount the method you wish to call Components2.vue // 挂载你想调用的方法


// new code
  mounted() {

    this.$root.$on("getProjects", () => {

      return this.getProjects(url = '/api/departments');
    });
  }


Component1.vue // call the mounted method Component1.vue // 调用挂载的方法

 methods: {
   // new code
    emitGetProjects() {
     this.$root.$emit("getProjects") //like this
    }
  },


more info here How can I call method in other component on vue.js 2?更多信息在这里如何在 vue.js 2 上调用其他组件中的方法? in case i made any typos万一我打错了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM