javascript - Vue.js 2.x 将函数传递给组件

标签 javascript vue.js vuejs2

我需要使用不同的信息多次渲染相同的组件,当有人选择某些内容时,它应该在主 Vue 实例中加载一个函数。

到目前为止,我无法将动态函数作为属性传递,例如 @change="onchange":

[Vue warn]: Invalid handler for event "change": got undefined

found in

---> at resources\assets\js\components\GeographyField.vue

组件:

<template>
    <fieldset class="form-group col-md">
        <label :for="name" class="control-label" v-html="label"></label>
        <select class="form-control" :name="name" :id="name" :model="_default" :disabled="disabled" @change="onchange">
            <option :value="0" v-html="placeholder"></option>
            <option v-for="object in collection" :value="object.id">{{ object.name }}</option>
        </select>
    </fieldset>
</template>

<script>
    module.exports = {
        props: {
            label: {
                type: String,
                default: 'Options'
            },
            _default: {
                type: Number,
                default: 0
            },
            disabled: {
                type: Boolean,
                default: false
            },
            placeholder: {
                type: String,
                default: 'Choose an option'
            },
            name: {
                type: String,
                required: true,
            },
            collection: {
                type: Array,
                required: true
            },
            onchange: {

            }
        },
        data: function() {
            return {

            }
        },
        methods: {

        }
    }
</script>


渲染:

<div class="row">
    <!-- Country -->
    <geography-field
            label="Pa&iacute;s"
            :_default="selectedCountry"
            placeholder="Choose a country"
            name="country"
            :collection="countries"
            :onchange="selectCountry()"
    ></geography-field>
    <!-- Department -->
    <geography-field
            label="Departamento"
            :_default="selectedDepartment"
            :disabled="selectedCountry < 1"
            placeholder="Choose a department"
            name="department"
            :collection="departments"
            :onchange="selectDepartment()"
    ></geography-field>
    <!-- Location -->
    <geography-field
            label="Localidades"
            :_default="selectedLocation"
            :disabled="selectedDepartment < 1"
            placeholder="Choose a location"
            name="location"
            :collection="localities"
            :onchange="selectLocation()"
    ></geography-field>
    <!-- Zone -->
    <geography-field
            label="Zonas"
            :_default="selectedZone"
            :disabled="selectedLocation < 1"
            placeholder="Choose a zone"
            name="zone"
            :collection="zones"
    ></geography-field>
</div>

编辑:包括selectCountry():

selectCountry: function() {
        if(this.selectedCountry < 1) { return; }
        axios.get('/get_country/' + this.selectedCountry)
            .then(function (response) {
                var data = response.data;
                if(data.departments.length > 0) {
                    var departments = [];
                    $.each(data.departments, function (index, value) {
                        departments.push(value);
                    });
                    app.departments = departments;
                }
            });
    },

我应该如何正确地将函数传递给组件?任何建议都表示赞赏

编辑 2: 我可能要澄清的是,组件渲染得很好。以防万一我将添加组件注册:

Vue.component('geography-field', require('./components/GeographyField'));
...
const app = new Vue({

最佳答案

要将函数作为 prop 传递给组件,您需要去掉函数名称的尾部括号。否则,Vue 将评估该函数。例如:

<geography-field
  label="Pa&iacute;s"
  :_default="selectedCountry"
  placeholder="Choose a country"
  name="country"
  :collection="countries"
  :onchange="selectCountry" // strip off the parentheses
></geography-field>
<小时/>

但是,我还建议您使用 $emit 而不是传递函数。你可以这样做:

组件定义:

<template>
  <fieldset class="form-group col-md">
    <label :for="name" class="control-label" v-html="label"></label>
    <select class="form-control" :name="name" :id="name" :model="_default" :disabled="disabled" @change="onchange">
      <option :value="0" v-html="placeholder"></option>
      <option v-for="object in collection" :value="object.id">{{ object.name }}</option>
    </select>
  </fieldset>
</template>

<script>
module.exports = {
  props: {
    label: { type: String, default: 'Options' },
    _default: { type: Number, default: 0 },
    disabled: { type: Boolean, default: false },
    placeholder: { type: String, default: 'Choose an option' },
    name: { type: String, required: true },
    collection: { type: Array, required: true }
  },
  methods: {
    onchange() {
      this.$emit("onchange"); 
    }
  }
}
</script>

父作用域中的组件标记:

<geography-field
  label="Pa&iacute;s"
  :_default="selectedCountry"
  placeholder="Choose a country"
  name="country"
  :collection="countries"
  @onchange="selectCountry" // use @
></geography-field>

关于javascript - Vue.js 2.x 将函数传递给组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48757903/

相关文章:

javascript - Vue.js - 具有内联样式绑定(bind)的动态进度条

javascript - 将 keyDown 事件添加到 iframe 中的 Canvas

javascript - 动态加载 reCAPTCHA

javascript - 在句子末尾插入随机字符

vue.js - Vue 创建项目 - 找不到模块 'vue-loader-v16/package.json'

vue.js - 从 api 返回 promise 并放入全局数据

javascript - Laravel 5.3.10 + Vue.js 2.0.1 上出现 "Failed to mount component” 错误

vuejs2 - 默认选择选项未在 DOM 中更新,即使 v-model 属性正在更新

javascript - 循环时通过 JavaScript 将数据传递到弹出页面

Vue.js 选择的选项不是基于值