简体   繁体   English

如何从 Vue 实例外部触发 Stencil 组件功能/方法

[英]How to trigger Stencil component function/method from outside Vue instance

Is there any possibility to trigger Stencil compontent function from parent Vue instance?是否有可能从父 Vue 实例触发 Stencil 组件 function?

I would like to, for example, run callChild() method in Vue instance, that will trigger doSomething() in Stencil component.例如,我想在 Vue 实例中运行callChild()方法,这将触发 Stencil 组件中的doSomething()

Main, html file with vue code file, which contains stencil component:主要,html 文件和 vue 代码文件,其中包含模板组件:

<body>
    <div id="app">
      <test-component :rules="[required, passwordRule]" :placeholder="placeholder" :label="label" :value="value" @valueChange="e => onValueChange"></test-component>
      {{value}}
      <button @click="foo()">Test</button>
    </div>
  </body>
  <script>
    var app = new Vue({
      el: '#app',
      data() {
        return {
          label: 'Nazwa Użytkownika',
          value: '',
          placeholder: 'Wpisz nazwę użytkownika',
          required: v => !!v || 'This field is required',
          passwordRule: v => v.length >= 8 || 'Your password is too short',
        };
      },
      methods: {
        callChild() {
          //this function should trigger bar() function in stancil <test-component>
        },
        onValueChange(e) {
          console.log(e);
        },
      },
    });
  </script>

Stencil component code:模板组件代码:

import { h, Component, Element, Event, EventEmitter, State, Watch, Prop, Method } from '@stencil/core';

@Component({
  tag: 'test-component',
  styleUrl: 'test-component.css',
  //shadow: true,
})
export class FormInputBase {
  @Element() el: HTMLElement;
  @Prop() type: string = 'text';
  @Prop() label: string;
  @Prop() rules: Array<Function>;
  @Prop() placeholder: string;
  @Prop({ mutable: true }) value: string;
  @Prop() hidedetails: boolean;
  @Event() valueChange: EventEmitter;

  @State() errorDetails: any;
  @State() showError: boolean = false;

  @Watch('value')
  watchHandler(newValue: string, oldValue: string) {
    console.log('The new value of activated is: ', newValue, 'Old val: ', oldValue);
    console.log(this.required(newValue));
    if (this.required(newValue) != true) {
      this.errorDetails = this.required(newValue);
      this.showError = true;
    } else {
      this.showError = false;
    }

    console.log(this.rules);
  }

  required = v => !!v || 'This field is required';

  @Method()
  async testClick(val) {
    await console.log(val);
  }

  @Method()
  doSomething() {
    //do something
  }

  handleChange(event) {
    const val = event.target.value;
    console.log(val);
    this.value = val;
    this.valueChange.emit(val);
  }
  render() {
    if (this.hidedetails) {
      return (
        <div>
          <label>
            {this.label}
            <div>
              <input placeholder={this.placeholder} value={this.value} onInput={event => this.handleChange(event)}></input>
            </div>
          </label>
        </div>
      );
    } else {
      return (
        <div>
          <label>
            {this.label}
            <div>
              <input placeholder={this.placeholder} value={this.value} onInput={event => this.handleChange(event)}></input>
              <div class="error-details">
                <div class={this.showError ? 'text-active' : 'text-inactive'}>{this.errorDetails}</div>
              </div>
              <button onClick={this.testClick}>Kilknij mnie!</button>
            </div>
          </label>
        </div>
      );
    }
  }
}

Ok - the answer is simple.好的 - 答案很简单。 You need to use ref.您需要使用参考。 Just add ref tag to element in html:只需将 ref 标签添加到 html 中的元素:

<test-component ref="form" :rules="[required, passwordRule]" :placeholder="placeholder" :label="label" :value="value" @valueChange="e => onValueChange"></test-component>

And then Vue method should look like:然后 Vue 方法应该如下所示:

foo() {
          this.$refs.form.bar();
        },

Where bar() is exactly name of method in Your Stencil component.其中 bar() 正是您的模板组件中的方法名称。

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

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