在Vue项目中使用Bootstrap Switch,vue-switches , vue-bootstrap-switch

在Vue中使用Bootstrap Switch(已经引入jQuery)有三个步骤:

  1. 首先,安装Bootstrap和Bootstrap Switch的相关资源。可以通过以下方式之一进行安装:

    使用包管理器(如npm)安装:
    npm install bootstrap-switch

    或者直接在HTML文件中引入Bootstrap Switch的CDN链接:
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-switch@3.3.4/dist/css/bootstrap3/bootstrap-switch.min.css">

    <script src="https://cdn.jsdelivr.net/npm/bootstrap-switch@3.3.4/dist/js/bootstrap-switch.min.js"></script>
     
  2. 在模板中使用Bootstrap Switch组件:
    <div class="form-check">
    <input class="form-check-input" type="checkbox" id="mySwitch" data-toggle="switch">
    <label class="form-check-label" for="mySwitch">Toggle Switch</label>
    </div>

  3. 在Vue组件中使用Bootstrap Switch。可以通过以下方式之一完成:

    在Vue组件的mounted生命周期钩子中初始化Bootstrap Switch:

    mounted() { // 初始化Bootstrap Switch
    $("[data-toggle='switch']").bootstrapSwitch(); }

    如果您使用的是Vue CLI等构建工具,可以在组件的created生命周期钩子中使用import导入Bootstrap Switch并进行初始化:
    import 'bootstrap-switch/dist/css/bootstrap3/bootstrap-switch.min.css';
    import 'bootstrap-switch/dist/js/bootstrap-switch.min.js';
    export default {
    created() { // 初始化Bootstrap Switch
    $("[data-toggle='switch']").bootstrapSwitch(); } }

vue-switches库使用:

  1. npm install vue-switches

  2. 在Vue组件中引入和注册vue-switches组件:

    <template>
       <div>
           <vue-switch v-model="switchValue"
    text-enabled="在线" text-disabled="离                       线" theme="bulma" color="green" @change="handleSwitchChange" />   
    </div>
    </template>
    <script> import VueSwitch from 'vue-switches';
        export default { components: { VueSwitch, },
        data() { return { switchValue: false, }; }, };
    </script>

     

使用vue-bootstrap-switch(该库已被移除或重命名组件库来实现类似的功能

  1. 安装vue-bootstrap-switch库。可以使用包管理器(如npm)进行安装:

    npm install vue-bootstrap-switch

  2. 在Vue组件中引入和注册vue-bootstrap-switch组件:
    <template>
    <div> <bootstrap-switch v-model="switchValue" /> </div>
    </template>
    <script> import BootstrapSwitch from 'vue-bootstrap-switch';
    export default {
         components: { BootstrapSwitch, },
         data() { return { switchValue: false,};},
         };
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值