Emberjs-comp-block-nonblock
提供:Dev Guides
ブロックと非ブロックの両方のコンポーネントの使用をサポート
_hasBlock_プロパティを使用して、単一コンポーネントからのブロックコンポーネントと非ブロックコンポーネントの使用をサポートできます。
構文
{{#if hasBlock}}
//code here
{{/if}}
例
以下の例では、1つのテンプレートでブロックコンポーネントと非ブロックコンポーネントの両方の使用をサポートすることを指定しています。 _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 () {
return {
title: "Emberjs",
author: "finddevguides",
body: "This is introduction"
};
}
});
_comp-yield_という名前のコンポーネントを作成し、次のコードで_app/templates/_の下に作成されたコンポーネントテンプレートファイル_comp-yield.hbs_を開きます-
{{#comp-yield title = title}}
<p class = "author">by (blocked name){{author}}</p>
{{body}}
{{/comp-yield}}
{{comp-yield title = title}}
{{outlet}}
_app/templates/components/_の下に作成された_comp-yield.hbs_ファイルを開き、次のコードを入力します-
{{#if hasBlock}}
<div class = "body">{{yield}}</div>
{{else}}
<div class = "body">finddevguides data is missing</div>
{{/if}}
{{yield}}
出力
emberサーバーを実行します。次の出力が表示されます-
Ember.jsコンポーネントブロックおよび非ブロックコンポーネントの使用
リンクをクリックすると、下のスクリーンショットに示すように名前がブロックされます-