stepper开发指导
===========

当一个任务需要多个步骤时，可以使用stepper组件展示当前进展。具体用法请参考[stepper API](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-container-stepper)。

#### 创建stepper组件

在pages/index目录下的hml文件中创建一个stepper组件。

<!-- xxx.hml -->
<div class="container"> 
 <stepper>    
   <stepper-item>     
     <text>Step 1</text>
   </stepper-item> 
   <stepper-item>     
     <text>Step 2</text>
   </stepper-item> 
 </stepper> 
</div>

/\* xxx.css \*/
.container {
  width:100%;
  height:100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/1a/v3/RCsiR9wCRzecTUeA1UEJMA/zh-cn_image_0000002664207869.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=948651509AC256B23F7C9D39DA587C052A9C7C8905BDB82F98232B67E65F848B)

#### 设置index和label属性

页面默认显示索引值为index的步骤。

<!-- xxx.hml -->
<div class="container"> 
 <stepper index="2">    
   <stepper-item>     
     <text>stepper-item1</text>
   </stepper-item> 
   <stepper-item>     
     <text>stepper-item2</text>
   </stepper-item> 
   <stepper-item>     
     <text>stepper-item3</text>
   </stepper-item> 
  </stepper> 
</div>

/\* xxx.css \*/
.container {
  width:100%;
  height:100%;
  flex-direction: column;
  background-color: #F1F3F5;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/0f/v3/n-xDI0hAQdeyfzQ_BjXFRw/zh-cn_image_0000002664327929.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=BE7BCF5AB2A436757FC4E540E51064642FFD913B11B1DBEBE443AD2CB59D1430)

通过设置label属性，自定义stepper-item的提示按钮。

<!-- xxx.hml -->
<div class="container"> 
 <stepper index="1">    
   <stepper-item label="{{label\_1}}">     
     <text>stepper-item1</text>
   </stepper-item> 
   <stepper-item label="{{label\_2}}">     
     <text>stepper-item2</text>
   </stepper-item> 
   <stepper-item label="{{label\_3}}">     
     <text>stepper-item3</text>
   </stepper-item>
   <stepper-item>     
     <text>stepper-item4</text>
   </stepper-item> 
 </stepper> 
</div>

/\* xxx.css \*/
.container {
  width:100%;
  height:100%;
  flex-direction: column;
  background-color: #F1F3F5;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}

// xxx.js
export default {
  data: {
    label\_1:{
      nextLabel: 'NEXT',
      status: 'normal'
    },
    label\_2:{
      prevLabel: 'BACK',
      nextLabel: 'NEXT',
      status: 'normal'
    },
    label\_3:{
      prevLabel: 'BACK',
      nextLabel: 'END',
      status: 'disabled'
    },
  },
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/9d/v3/2hI_WK8eSo-5bJbgdLs-tQ/zh-cn_image_0000002633848820.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=F7AA7AF548BA667428654592D8376CD0A0630317ED9FE9354BA8767672230E18)

#### 设置样式

stepper组件默认填充父容器，通过border和background-color设置边框、背景色。

<!-- xxx.hml -->
<div class="container" > 
  <div class="stepperContent">
    <stepper class="stepperClass">    
      <stepper-item>     
        <text>stepper-item1</text>
      </stepper-item> 
    </stepper> 
  </div>
</div>

/\* xxx.css \*/
.container {
  width:100%;
  height:100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color:#F1F3F5;
}
.stepperContent{
  width: 300px;
  height: 300px;
}
.stepperClass{
  border:1px solid silver ;
  background-color: white;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f2/v3/qbQB94muQ5-3iAbIh7gLWg/zh-cn_image_0000002634008724.png?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=93B66F930AAE5430F98BA63A144D9826A7E58DFAE9172C8FAD55C0D62F178B0E)

#### 添加事件

stepper分别添加finish，change，next，back，skip事件。

*   当change与next或back同时存在时，会先执行next或back事件再去执行change事件。
    
*   重新设置index属性值时要先清除index的值再重新设置，否则检测不到值的改变。
    

<!-- xxx.hml -->
<div class="container"  style="background-color:#F1F3F5;">
  <div >
    <stepper onfinish="stepperFinish" onchange="stepperChange" onnext="stepperNext" onback="stepperBack" onskip="stepperSkip" id="stepperId" index="{{index}}">
      <stepper-item>
        <text>stepper-item1</text>
        <button value="skip" onclick="skipClick"></button>
      </stepper-item>
      <stepper-item>
         <text>stepper-item2</text>
         <button value="skip" onclick="skipClick"></button>
      </stepper-item>
      <stepper-item>
        <text>stepper-item3</text>
      </stepper-item>
    </stepper>
  </div>
</div>

/\* xxx.css \*/
.container {
  width:100%;
  height:100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
stepper-item{
  width: 100%;
  flex-direction: column;
  align-self: center;
  justify-content: center;
}
text{
  margin-top: 45%;
  justify-content: center;
  align-self: center;
  margin-bottom: 50px;
}
button{
  width: 80%;
  height: 60px;
  margin-top: 20px;
}

// xxx.js
import promptAction from '@ohos.promptAction';
export default {
  data: {
    index:0,
  },
   stepperSkip(){
    this.index=2;
  },
   skipClick(){
    this.$element('stepperId').setNextButtonStatus({status: 'skip', label: 'SKIP'});
  },
  stepperFinish(){
    promptAction.showToast({
      message: 'All Finished'
    })
  },
  stepperChange(e){
    console.info("stepperChange"+e.index)
    promptAction.showToast({
      // index表示当前步骤的序号
      message: 'Previous step: '+e.prevIndex+"-------Current step:"+e.index
    })
  },
  stepperNext(e){
    console.info("stepperNext"+e.index)
    promptAction.showToast({
      // pendingIndex表示将要跳转的序号
      message: 'Current step:'+e.index+"-------Next step:"+e.pendingIndex
    })
    var index = {pendingIndex:e.pendingIndex }
    return index;
  },
  stepperBack(e){
    console.info("stepperBack"+e.index)
    var index = {pendingIndex: e.pendingIndex }
    return index;
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/27/v3/1VYg_AWkQhigtm9cL0ccuw/zh-cn_image_0000002664207871.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=D1A1CD1223FA6E189630E41847A0F8AB042E9241F38FF22E5DF66CD56211195F)