Emberjs-comp-return-val-yield
提供:Dev Guides
歩留まりのあるコンポーネントから値を返す
_yield_オプションを使用して、コンポーネントから値を返すことができます。
構文
{#each myval as |myval1|}}
{{ yield myval1 }}
{{/each}}
例
以下の例は、_yield_プロパティを使用してコンポーネントから値を返すことを指定しています。 _comp-yield_という名前のルートを作成し、_router.js_ファイルを開いてURLマッピングを定義します-
import Ember from 'ember';
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config
//The const declares read only variable
const Router = Ember.Router.extend ({
location: config.locationType,
rootURL: config.rootURL
});
//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
this.route('comp-yield');
});
export default Router;
_application.hbs_ファイルを作成し、次のコードを追加します-
//link-to is a handlebar helper used for creating links
{{#link-to 'comp-yield'}}Click Here{{/link-to}}
{{outlet}}//It is a general helper, where content from other pages
will appear inside this section
_app/routes/_の下に作成された_comp-yield.js_ファイルを開き、次のコードを入力します-
import Ember from 'ember';
export default Ember.Route.extend ({
model: function() {
//an array called 'country' contains objects
return { country: ['India', 'England', 'Australia'] };
}
});
comp-yieldという名前のコンポーネントを作成し、次のコードで_app/templates/_の下に作成されたコンポーネントテンプレートファイル_comp-yield.hbs_を開きます-
{{#comp-yield country=model.country as |myval|}}
<h3>{{ myval }}</h3>
{{/comp-yield}}
{{outlet}}
_app/templates/components/_の下に作成された_comp-yield.hbs_ファイルを開き、次のコードを入力します-
<h2>List of countries are:</h2>
//template iterates an array named 'country'
{{#each country as |myval|}} //each item in an array provided as blobk param 'myval'
{{ yield myval }}
{{/each}}
出力
emberサーバーを実行します。次の出力が表示されます-
リンクをクリックすると、下のスクリーンショットに示すように、配列からオブジェクトのリストが表示されます-