微信小程序组件传参设置默认值
时间: 2024-10-15 19:15:55 浏览: 169
微信小程序中的组件传参通常通过`props`属性来进行。如果你需要给组件设置默认值,可以在接收传参的地方进行初始化。例如,假设你有一个`my-button`组件,它接受一个名为`title`的参数:
```html
<view class="parent">
<my-button title="{{ defaultTitle || '点击我' }}"></my-button>
</view>
```
在这个例子中,`defaultTitle`是一个在父组件里可以赋值的变量,默认值设为 `'点击我'`。如果`title`从父组件传递过来,那么它的值会覆盖默认值;如果没有传递,则使用默认值。
如果你想让这个默认值成为全局的,可以在`App.js`文件的`Page`配置项或者全局变量中定义:
```javascript
Page({
data: {
defaultTitle: '点击我'
},
childComponentDidMount: function() {
// 在子组件渲染前设定默认值
this.setData({ defaultTitle: '点击我' });
}
})
```
阅读全文
相关推荐

















