课程表

Omi课程

工具箱
速查手册

Omi 条件判断

当前位置:免费教程 » JS/JS库/框架 » Omi

我们经常需要根据不同的状态呈现不同的界面,比如有的用户是vip要显示vip的Logo。Omi有许多种方式满足你的要求。

方式一

  1. class ConditionTest extends Omi.Component {
  2. constructor(data) {
  3. super(data);
  4. }
  5. render () {
  6. return `{{#isVip}}
  7. <div>you are VIP.</div>
  8. {{/isVip}}
  9. {{^isVip}}
  10. <div>you are not VIP.</div>
  11. {{/isVip}}`;
  12. }
  13. }

上面完全使用mustachejs的条件判断的语法。当然Omi不强制你使用mustachejs。你可以是omi.lite.js,然后重写Omi.template方法去使用任意你喜爱的模板引擎。

方式二

  1. class ConditionTest extends Omi.Component {
  2. constructor(data) {
  3. super(data);
  4. }
  5. render () {
  6. if(this.data.isVip){
  7. return '<div>you are VIP.</div>';
  8. }else{
  9. return '<div>you are not VIP.</div>';
  10. }
  11. }
  12. }

render就是提供了很好的可编程性,里面可以写任意js逻辑代码。对了,差点忘了,style方法里面也可以写js逻辑的。

  1. class ConditionTest extends Omi.Component {
  2. constructor(data) {
  3. super(data);
  4. }
  5. style (){
  6. if(this.data.isVip){
  7. return 'div{ color : red; }';
  8. }else{
  9. return 'div{ color : green; }';
  10. }
  11. }
  12. render () {
  13. if(this.data.isVip){
  14. return '<div>you are VIP.</div>';
  15. }else{
  16. return '<div>you are not VIP.</div>';
  17. }
  18. }
  19. }
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号