Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

vue组件 - 函数属性值的传递问题

测试地址:https://codesandbox.io/s/js-p...

给自定义组件的 onChange 属性传递函数,

  <form-input 
    type="text"
    label="标签"
    :value="inputVal"
    :onChange="onChange"
  />

函数定义在 methods 域,

new Vue({
  el: '#container',
  data() {
    return {
      inputVal: 'haha'
    }
  },
  methods: {
    onChange(p) {
      console.log('jafas')
    }
  }
});

可是事件触发时却报函数需要名字的错误,实在想不明白为什么。
image

自定义组件如下:

Vue.component('form-input', {
  props: {
    label: String,
    value: String,
    onChange: {
      type: Function,
      default: () => {}
    },
    placeholder: String,
    type: {
      type: String,
      default: 'text'
    },
  },
  template: `
      <div class="bzq_input">
        <label :for="label" class="form-label">{{label}}</label>
        <input :type="type" class="form-control" :id="label" :placeholder="placeholder"
        :value="value"
        @change="onChange"
        >
      </div>
    `
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

on-change
在html中需要用短横线形式


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...