亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何迭代車把中對象的屬性

如何迭代車把中對象的屬性

長風秋雁 2023-11-02 21:56:39
{  user: {  photos: ['a','b']    }}你如何迭代photos. 像這樣的東西{{#each user.photos}}   <span>{{this}}</span>{{/each}}
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

確保:


檢查您的 HTML 中是否存在該模板:

<script id="user-photos-template" type="text/x-handlebars-template">

{{#each user.photos}}

  <span class="user-photo">{{this}}</span>

{{/each}}

</script>

編譯模板:

const template = (sel) => Handlebars.compile(document.querySelector(sel).innerHTML);

const userPhotosTemplate = template('#user-photos-template');

設置渲染數據的 HTML 文本:

document.getElementById('result').innerHTML = userPhotosTemplate(userData);

例子

const template = (sel) => Handlebars.compile(document.querySelector(sel).innerHTML);

const userPhotosTemplate = template('#user-photos-template');

const userData = {

  user: {

    name: 'Bob',

    photos: ['a', 'b', 'c']

  }

};


document.getElementById('result').innerHTML = userPhotosTemplate(userData);

.user {

  display: flex;

  flex-direction: column;

  justify-content: center;

  width: 10em;

  height: 10em;

  border: thin solid grey;

}


.user .user-name {

  font-weight: bold;

  text-align: center;

  margin-top: 0.25em;

}


.user .user-photos {

  display: flex;

  flex-direction: row;

  flex-wrap: wrap;

  justify-content: center;

  flex: 1;

}


.user .user-photos .user-photo {

  width: 3em;

  height: 3em;

  line-height: 3em;

  margin: 0.5em;

  border: thin solid grey;

  text-align: center;

  text-transform: uppercase;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.6/handlebars.min.js"></script>

<div id="result"></div>

<script id="user-photos-template" type="text/x-handlebars-template">

<div class="user">

  <div class="user-name">{{user.name}}</div>

  <div class="user-photos">

  {{#each user.photos}}

     <span class="user-photo">{{this}}</span>

  {{/each}}

  </div>

</div>

</script>


查看完整回答
反對 回復 2023-11-02
  • 1 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號