代码之家  ›  专栏  ›  技术社区  ›  HalfWebDev

从jsxvue中编写的子组件调用父组件方法

  •  1
  • HalfWebDev  · 技术社区  · 6 年前

    我有一个子组件

    <script>
    import StyledInput from "./input";
    
    export default {
      name: "MyInput",
      props: {
        multiline: Boolean,
        onChange: Function
      },
      render(h) {
        const { multiline, onChange } = this;
        console.log(onChange);
        return (
          <div>
            <StyledInput multiline={multiline} onChange={e => onChange(e)} />
          </div>
        );
      }
    };
    </script>
    

    然后我将实际输入作为vue样式的组件 输入.js . 对于那些熟悉样式化组件的人来说,不需要解释代码。

    然后在父组件Home.vue中使用这个InputWrapper组件

    <template>
    
    
    <div class="pLR15 home">
          <Row>
              <MyInput :multiline="true" placeholder="Sample Input" type="text" :onChange="handleChange"></MyInput> 
          </Row>
      </div>
    </template>
    
    <script>
    // @ is an alias to /src
    import HelloWorld from "@/components/HelloWorld.vue";
    import Row from "@/ui_components/row";
    import MyInput from "@/ui_components/form/input/index.vue";
    
    export default {
      name: "home",
      components: {
        HelloWorld,
        Row,
        MyInput
      },
      methods: {
        handleChange: function(e) {
          console.log("Hey you are changing my value to", e.target.value);
        }
      }
    };
    </script>
    

    -未激发父级上的onChange。

    1 回复  |  直到 6 年前
        1
  •  0
  •   HalfWebDev    6 年前

    @change.native="handleChange" 为我做了这个把戏。我还清理了所有监听child的事件处理程序。自从我的 type placeholder .本地 修饰符目前效果最好。尽管我之前尝试过的@change=“handleChange”可以使事情变得一致,并导致尝试将函数作为道具传递给child。

    IMP NOTE-Vuejs不会将@change视为普通onChange。 @输入