简体   繁体   English

如何从其他组件调用 Ember 组件?

[英]How to call Ember component from other component?

I have these components and I want them can be call from other component.我有这些组件,我希望它们可以从其他组件调用。

templates/components/post-edit.hbs模板/组件/post-edit.hbs

<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <form id="{{if isEditing 'form-edit' 'form-add'}}" method="post" enctype="multipart/form-data">
                <div class="form-result-placeholder"></div><!--.form-result-placeholder-->
                <div class="form-group">
                    <label for="name">Name</label>
                    {{input id="name" class="form-control" name="name" placeholder="Name" value=name required="required"}}
                </div>
                <div class="form-group">
                    <label for="file">File</label>
                    {{input type="hidden" name="MAX_FILE_SIZE" value="1048576"}}
                    {{input id="file" class="form-control" type="file" name="file" aria-describedby="file_help"}}
                    <small id="file_help">JPG only, less than 1 MB.</small>
                </div>
                <button type="submit" {{action 'addNew'}} class="btn btn-primary">Submit</button>
                <a href="./" class="btn btn-secondary">Go back</a>
            </form>
        </div>
    </div>
</div>

components/post-edit.js组件/post-edit.js

import Ember from 'ember';
import config from 'php-api-ember-test/config/environment';

export default Ember.Component.extend({
    actions: {
        addNew() {
            console.log('hit on submit button from add page');
            this.get('addNew');
        },
        editSave() {
            console.log('hit on submit button from edit page');
            this.get('editSave');
        }
    },
    addNew: Ember.computed(function() {
        console.log('adding new data');
        var formData = new FormData($('#form-add')[0]);
        this.get('ajax').request(config.APP.phpApiUrl+'/add', {
            method: 'POST',
        })
        .catch(function(error) {
            console.log(error);
            var response = error.errors[0].detail;
            console.log(response);
            // I want to call other component and render from here and put it in form-result-placeholder class.
            //console.log(Ember.Component.FormAlert);// undefined
            //$('.form-result-placeholder').html('{{form-alert}}');// not working
        });
    }),
    editSave: Ember.computed(function() {
        console.log('saving edit data');
    }),
    ajax: Ember.inject.service(),
    isEditing: false,
});

templates/components/form-alert.hbs模板/组件/form-alert.hbs

FORM ALERT!!! (this alert message will be change by ajax response. it is not static.)

components/form-alert.js组件/form-alert.js

import Ember from 'ember';

const FormAlert = Ember.Component.extend({
});

export default FormAlert;

I want to call form-alert component from inside post-edit.js file after ajax request.我想在ajax请求后从post-edit.js文件中调用form-alert组件。
I want to call other component and render after ajax request and put it in form-result-placeholder class.我想在ajax请求之后调用其他组件并渲染并将其放入form-result-placeholder类中。
How to call Ember component from other component and render into target element?如何从其他组件调用 Ember 组件并渲染到目标元素?

My Ember version: 2.8.0我的 Ember 版本:2.8.0

Take a look at this question.看看这个问题。 You need to use block form, and set the action on the child component.您需要使用块形式,并在子组件上设置操作。

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

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