动态组件&插槽

博客介绍了Vue的动态组件和插槽slot。动态组件可改变,通过Vue的component+is属性使用,还可搭配keep-alive组件缓存以提高效率。插槽slot用于预先保留内容,具名插槽在vue2.6以上被废弃,建议掌握新的v-slot指令,同时旧的也需了解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态组件

  1. 什么是动态组件?
    可以改变的组件
  2. 使用
    通过Vue提供一个component+is属性
  3. 动态组件指的就是component这个组件
  4. 案例-业务:点击一个按钮进行组件的切换
<div id="app">
    <button @click="change">切换</button>
    <keep-alive include="">
        <component :is="type"></component>
    </keep-alive>
</div>

<script>
Vue.component('Aa',{
    template:'<div>Aa</div>'
})
Vue.component('Bb',{
    template:'<div>Bb</div>'
})

new Vue({
    data:{
        type:Aa
    },
    method:{
        change(){
            this.type=(this.type==='Aa'?'Bb':'Aa')
        }
    }
}).$mount('#app')
</script>
  1. Vue提供了一个keep-alive的组件可以将我们的组件进行浏览器缓存,这样当我们切换组件时,就可以大大提高使用效率
  2. keep-alive也可以使用属性的形式呈现,但是如果我们搭配component的话,建议使用组件的形式。

插槽slot

  1. 作用/概念:预先将将来要使用的内容进行保留
  2. 具名插槽:给slot取个名字
  • 注意:以上两种形式在vue2.6以上被废弃
  • 为什么要用v-slot指令来代替呢?
  • 将具名插槽和作用域插槽进行统一
  • 要将这两个属性带有vue的标志,并且符合vue两个最大的特性之一:指令的概念
  • 建议:新的v-slot要掌握,旧的也掌握吧。
  1. 案例:
    <div id="app">
        <!--Hello组件-->
        <Hello>
            <div>
                这是填补内容
            </div>
        </Hello>
    </div>

    <!--template-->
    <template id="hello">
        <div>
            <slot></slot>
            <h3>this is Hello</h3>
        </div>
    </template>

    <script>
    Vue.componnet('Hello',{
        template:'#hello'
    })

    new Vue({
        el:'#app'
    })
    
    </script>
  1. 具名插槽
<div id="app">
    <Hello>
        <header slot="header">this is header</header>
        <footer slot="footer">this is footer</footer>
    </Hello>
</div>

<template id="hello">
<div>
    <slot name="header"></slot>
    <h3>this is content</h3>
    <slot name="footer"></slot>
</div>
</template>

<script>
    Vue.component('Hello',{
       template:'#hello' 
    })
    new Vue({

    }).$mount('#app')
</script>
  1. v-slot (vue 2.6)
  • 案例:
<div id="app">
    <Hello>
       <template v-slot="header">
            <h1>this is title</h1>
       </template>

       <p>A paragraph for the main content.</p>
       <p>And another one.</p>
       <template v-slot="footer">
            <h1>this is footer</h1>
       </template>
    </Hello>
</div>

<template id="hello">
    <div class="container">
        <header>
            <slot name="header"></slot>
        </header>
        <footer>
            <slot name="footer"></slot>
        </footer>
    </div>
</template>
<script>

  Vue.component('Hello',{
    template: '#hello'
  })

  new Vue({
    el: '#app',
  })
</script>

div>


组件是 Vue.js 中最重要的概念之一。组件可以让我们将 UI 拆分为独立、可复用的部件,使得代码更加清晰、易于维护。在 Vue.js 中,组件可以分为全局组件和局部组件,其中全局组件可在任何地方使用,而局部组件只能在其父组件中使用。 定义组件时,需要使用 Vue.component() 方法,该方法需要传入两个参数:组件名称和组件配置对象。组件名称应该采用 kebab-case(短横线分隔命名)格式,以便在 HTML 中使用。 示例代码如下: ```javascript // 定义一个名为 button-counter 的新组件 Vue.component(&#39;button-counter&#39;, { data: function () { return { count: 0 } }, template: &#39;&lt;button v-on:click=&quot;count++&quot;&gt;You clicked me {{ count }} times.&lt;/button&gt;&#39; }) ``` 在上述代码中,我们定义了一个名为 button-counter 的组件,该组件包含一个计数器,每次点击按钮计数器加一。 在 HTML 中使用组件时,需要使用组件名称作为自定义标签来调用组件。示例代码如下: ```html &lt;div id=&quot;app&quot;&gt; &lt;button-counter&gt;&lt;/button-counter&gt; &lt;/div&gt; ``` 在上述代码中,我们调用了 button-counter 组件,并将其渲染到了 id 为 app 的 div 元素中。 除了组件的 data 和 template 属性外,还可以使用 props 属性来传递组件之间的数据。使用 props 时,需要在组件的配置对象中定义 props 属性,并在 HTML 中使用 v-bind 指令来传递数据。 示例代码如下: ```javascript // 定义一个名为 todo-item 的新组件 Vue.component(&#39;todo-item&#39;, { props: [&#39;todo&#39;], template: &#39;&lt;li&gt;{{ todo.text }}&lt;/li&gt;&#39; }) // 创建一个 Vue 实例 var app = new Vue({ el: &#39;#app&#39;, data: { groceryList: [ { id: 0, text: &#39;蔬菜&#39; }, { id: 1, text: &#39;水果&#39; }, { id: 2, text: &#39;奶酪&#39; } ] } }) ``` 在上述代码中,我们定义了一个名为 todo-item 的组件,并使用 props 属性定义了一个名为 todo 的 prop。在 HTML 中,我们使用 v-bind 指令将 groceryList 数组中的每个对象传递给了 todo-item 组件。示例代码如下: ```html &lt;div id=&quot;app&quot;&gt; &lt;ul&gt; &lt;todo-item v-for=&quot;item in groceryList&quot; v-bind:todo=&quot;item&quot; v-bind:key=&quot;item.id&quot;&gt;&lt;/todo-item&gt; &lt;/ul&gt; &lt;/div&gt; ``` 在上述代码中,我们使用 v-for 指令遍历 groceryList 数组,并使用 v-bind 指令将数组中的每个对象传递给了 todo-item 组件。注意,我们还需要使用 v-bind:key 指令来为每个列表项指定一个唯一的 key 值。 插槽是 Vue.js 中另一个重要的概念。插槽可以让父组件在子组件中插入任意的 HTML 内容,使得组件更加灵活、可复用。 在子组件中,使用 &lt;slot&gt; 标签来定义插槽。在父组件中,使用子组件的自定义标签来调用组件,并在标签内部插入 HTML 内容。示例代码如下: ```javascript // 定义一个名为 alert-box 的新组件 Vue.component(&#39;alert-box&#39;, { template: ` &lt;div class=&quot;alert-box&quot;&gt; &lt;strong&gt;Error!&lt;/strong&gt; &lt;slot&gt;&lt;/slot&gt; &lt;/div&gt; ` }) // 创建一个 Vue 实例 var app = new Vue({ el: &#39;#app&#39; }) ``` 在上述代码中,我们定义了一个名为 alert-box 的组件,并在组件中定义了一个插槽。在 HTML 中,我们调用了 alert-box 组件,并在标签内部插入了一些 HTML 内容。示例代码如下: ```html &lt;div id=&quot;app&quot;&gt; &lt;alert-box&gt; &lt;p&gt;Something bad happened.&lt;/p&gt; &lt;/alert-box&gt; &lt;/div&gt; ``` 在上述代码中,我们调用了 alert-box 组件,并在标签内部插入了一些 HTML 内容。该 HTML 内容会被插入到 alert-box 组件插槽中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值