uniapp中修改uni.showModal弹框content内容样式
时间: 2023-11-23 13:57:36 浏览: 286
浅析微信小程序modal弹窗关闭默认会执行cancel问题
要修改uni.showModal弹框content内容样式,可以按照以下步骤进行操作:
1. 在项目的common文件夹下的style.scss文件中定义全局样式,例如:
```scss
.uni-modal{
.uni-modal__bd{
color: blue;
font-size: 32rpx;
font-weight: bold;
padding: 64rpx 0 ;
}
}
```
2. 在main.js中引入show-modal组件,并创建组件show-modal.vue和initModal.js。
3. 在show-modal.vue中定义弹框的样式,例如:
```html
<template>
<div class="modal">
<div class="modal-content">{{content}}</div>
<div class="modal-buttons">
<button @click="cancel">取消</button>
<button @click="confirm">确定</button>
</div>
</div>
</template>
<script>
export default {
props: {
content: {
type: String,
default: ''
}
},
methods: {
cancel() {
this.$emit('cancel')
},
confirm() {
this.$emit('confirm')
}
}
}
</script>
<style scoped>
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
.modal-content {
background-color: #fff;
padding: 20px;
border-radius: 10px;
color: blue;
font-size: 32rpx;
font-weight: bold;
}
.modal-buttons {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
</style>
```
4. 在initModal.js中定义uni.showModal的样式,例如:
```js
import Vue from 'vue'
import ShowModal from './show-modal.vue'
function showModal(options) {
const Modal = Vue.extend(ShowModal)
const modal = new Modal({
el: document.createElement('div'),
propsData: {
content: options.content
}
})
document.body.appendChild(modal.$el)
return new Promise((resolve, reject) => {
modal.$on('cancel', () => {
modal.$el.remove()
reject('cancel')
})
modal.$on('confirm', () => {
modal.$el.remove()
resolve('confirm')
})
})
}
uni.showModal = showModal
```
5. 在需要使用uni.showModal的地方,直接调用uni.showModal即可,例如:
```js
uni.showModal({
title: '提示',
content: '这是一个自定义样式的弹框'
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
```
阅读全文