[英]How to call Ember component from other component?
我有這些組件,我希望它們可以從其他組件調用。
模板/組件/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>
組件/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,
});
模板/組件/form-alert.hbs
FORM ALERT!!! (this alert message will be change by ajax response. it is not static.)
組件/form-alert.js
import Ember from 'ember';
const FormAlert = Ember.Component.extend({
});
export default FormAlert;
我想在ajax請求后從post-edit.js文件中調用form-alert
組件。
我想在ajax請求之后調用其他組件並渲染並將其放入form-result-placeholder
類中。
如何從其他組件調用 Ember 組件並渲染到目標元素?
我的 Ember 版本:2.8.0
看看這個問題。 您需要使用塊形式,並在子組件上設置操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.