vue点击按钮切换颜色(v-bind的使用)

 <style type="text/css">
        div#box{
            width: 100px;
            height: 100px;
            color: darkgray;
        }
        .red{
            background-color: red;

        }
        .blue{
            background-color: blue;
        }
    </style>
</head>
<body>
<div id="app">
    <button @click="isRed=!isRed">点我切换颜色</button>

    <div id="box" v-bind:class="{red:isRed,blue:!isRed}"> 
        我是盒子

    </div>
</div>
<script src="node_modules/vue/dist/vue.js"></script>
<script>
   const app=new Vue({
       el:"#app",
       data:{

           isRed:true
           
       }
       
   });
</script>

要点:class要转变为vue的属性,加上v-bind:或者:,class的值可以是一个对象,对象里面可以写多个属性,借助button改变isRed的值来改变颜色。

 

### Vue3 动态组件 `v-bind` 使用方法 在 Vue3 中,动态组件可以通过 `<component>` 标签配合 `is` 特性和 `v-bind` 来实现。这种方式允许根据条件渲染同的组件,并可以传递属性给目标组件。 #### 基本概念 - **动态组件**:使用 `<component :is="someComponent">` 结构来决定哪个组件应该被渲染。 - **`v-bind`**:用于将一个对象作为一组属性应用到 HTML 或自定义组件上[^1]。 #### 实现方式 当结合两者时,通常会创建一个包含所需属性的对象,在模板中利用此对象一次性绑定多个 prop 到子组件上去: ```html <template> <!-- Dynamic component with bound properties --> <component :is="currentTabComponent" v-bind="propsObject"></component> </template> <script setup> import { ref } from &#39;vue&#39;; // Import your components here... const currentTabComponent = ref(MyDynamicComponent); const propsObject = { title: "Example Title", content: "This is an example of dynamic binding." }; </script> ``` 这段代码展示了如何设置一个响应式的变量 (`currentTabComponent`) 来控制当前显示的组件名称;而另一个对象(`propsObject`)则用来存储想要传入该组件的数据项。最后通过 `v-bind` 把这个对象里的键值对映射成实际参数发送出去[^2]。 对于更复杂的场景下,比如需要根据同情况调整传递的内容,则可以在计算属性内构建相应的配置对象再进行绑定操作: ```javascript computed: { computedProps() { return this.condition ? {...defaultProps, specialProp: true} : defaultProps; } } ``` 这样就可以灵活地改变所传递的具体内容而必每次都修改视图层逻辑[^3]。 #### 示例说明 假设有一个应用程序具有两个选项卡:“Home” 和 “About”。可以根据用户的交互切换两个页面之间的展示状态。此时便可以用上述提到的方法轻松完成需求: ```html <!-- TabSwitcher.vue --> <template> <div class="tabs"> <button @click="changeTab(&#39;home&#39;)">Home</button> <button @click="changeTab(&#39;about&#39;)">About</button> <component :is="activeTab.componentName" v-bind="activeTab.propsData"/> </div> </template> <script setup> import Home from &#39;./components/Home.vue&#39; import About from &#39;./components/About.vue&#39; let activeTab = reactive({ componentName: &#39;&#39;, propsData: {} }) function changeTab(tabName){ switch (tabName) { case &#39;home&#39;: Object.assign(activeTab,{ componentName: Home, propsData:{ message:"Welcome to the home page!" } }); break; case &#39;about&#39;: Object.assign(activeTab,{ componentName: About, propsData:{ description:"Learn more about us on our about page." } }); break; default: console.error("Invalid tab name"); } } onMounted(() => changeTab(&#39;home&#39;)) </script> ``` 在此案例里,每当点击按钮触发 `changeTab()` 方法后都会更新 `activeTab` 的值从而引起 UI 更新并加载对应的新组件及其初始化数据[^4]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值