System.register(["./vendor-legacy.d8a4b3f3.js"],(function(s){"use strict";var a,n,t;return{setters:[function(s){a=s.e,n=s.o,t=s.G}],execute:function(){const l={class:"markdown-body"},e=[t('

Switch 开关

介绍

用来打开或关闭选项。

安装

import { createApp } from 'vue';\n//vue\nimport { Switch } from '@nutui/nutui';\n//taro\nimport { Switch } from '@nutui/nutui-taro';\n\nconst app = createApp();\napp.use(Switch);\n\n

代码演示

基础用法

<nut-switch v-model="checked" />\n
import { ref } from 'vue';\nexport default {\n  setup() {\n    const checked = ref(true);\n    return { checked };\n  },\n};\n

禁用状态

<nut-switch v-model="checked" disable />\n

change事件

<nut-switch v-model="checked" @change="change" />\n
import { ref, getCurrentInstance } from 'vue';\nexport default {\n  setup() {\n    let { proxy } = getCurrentInstance() as any;\n    const checked = ref(true);\n    const change = (value: boolean, event: Event) => {\n      proxy.$toast.text(`触发了change事件,开关状态:${value}`);\n    };\n    return {\n      checked,\n      change\n    };\n  }\n};\n

异步控制

<nut-switch :model-value="checkedAsync" @change="changeAsync" />\n
import { ref, getCurrentInstance } from 'vue';\nexport default {\n  setup() {\n    let { proxy } = getCurrentInstance() as any;\n    const checkedAsync = ref(true);\n    const changeAsync = (value: boolean, event: Event) => {\n      proxy.$toast.text(`2秒后异步触发 ${value}`);\n      setTimeout(() => {\n        checkedAsync.value = value;\n      }, 2000);\n    };\n    \n    return {\n      checkedAsync,\n      changeAsync\n    };\n  }\n};\n

自定义颜色

<nut-switch v-model="checked" @change="switchChange" active-color="blue" />\n

支持文字

<nut-switch v-model="checked" @change="switchChange" active-text="开" inactive-text="关" />\n

API

Props

参数说明类型默认值
v-model开关状态Booleanfalse
disable禁用状态Booleanfalse
active-color打开时的背景颜色String#fa2c19
inactive-color关闭时的背景颜色String#ebebeb
active-text打开时文字描述String-
inactive-text关闭时文字描述String-

Events

事件名说明回调参数
change切换开关时触发(value: boolean,event: Event)
',26)];s("default",{setup:(s,{expose:t})=>(t({frontmatter:{}}),(s,t)=>(n(),a("div",l,e)))})}}}));