naive-ui/demo/documentation/components/modal/zhCN/controlled.demo.md

44 lines
739 B
Markdown
Raw Normal View History

2020-02-04 14:55:55 +08:00
# 受控显示
2020-09-13 19:08:54 +08:00
模态框的显示可以是受控的。
2020-02-04 14:55:55 +08:00
```html
<n-button
@click="handleClick"
>
来吧
2020-02-04 14:55:55 +08:00
</n-button>
2020-09-13 19:08:54 +08:00
<n-modal :show="showModal">
2020-02-04 14:55:55 +08:00
<n-card
style="width: 600px;"
title="模态框"
size="huge"
2020-09-13 19:08:54 +08:00
:bordered="false"
2020-02-04 14:55:55 +08:00
>
倒计时 {{ timeout / 1000 }} 秒
</n-card>
</n-modal>
```
```js
export default {
data () {
return {
2020-09-13 19:08:54 +08:00
showModal: false,
2020-02-04 14:55:55 +08:00
timeout: 6000
}
},
methods: {
handleClick () {
2020-09-13 19:08:54 +08:00
this.showModal = true
2020-02-04 14:55:55 +08:00
this.timeout = 6000
2020-09-13 19:08:54 +08:00
const countdown = () => {
2020-02-04 14:55:55 +08:00
if (this.timeout <= 0) {
2020-09-13 19:08:54 +08:00
this.showModal = false
2020-02-04 14:55:55 +08:00
} else {
this.timeout -= 1000
setTimeout(countdown, 1000)
}
}
countdown()
}
}
}
```