[Vue @Component] Switch Between Vue Components with Dynamic Components

本文介绍了一种使用Vue.js框架动态切换组件的方法。通过选择器下拉菜单来选择要渲染的组件,展示了如何利用Vue的特性实现简洁高效的动态组件替换。此示例包括三个不同的头部组件:One、Two 和 Three,分别呈现不同样式的标题。

A common scenario is to present different components based on the state of the application. Dynamic components in Vue make this simple by providing a component element that simply needs to be bound to the component that you want to render. This example shows using a select dropdown in Vue to select the component to render.

 

<template>
  <Settings>
    <Layout slot-scope="{header, footer}">
      <AwesomeHeader slot="header" :header="header"></AwesomeHeader>
      <div slot="content" class="flex-grow">
        <select v-model="selectedComp">
          <option v-for="comp in comps" :key="comp.name" :value="comp">{{comp.name}}</option>
        </select>
        <component :is="selectedComp"></component>
      </div>
      <AwesomeFooter slot="footer" :footer="footer"></AwesomeFooter>
    </Layout>
  </Settings>
</template>

<script>
import Vue from "vue"
import { Component, Prop } from "vue-property-decorator"
import Settings from "./Settings"
import Layout from "./Layout"
import { Header, Footer } from "./components"

const One = {
  functional: true,
  name: "One",
  render: h => <h1 class="bg-red">One</h1>
}

const Two = {
  functional: true,
  name: "Two",
  render: h => <h1 class="bg-green">Two</h1>
}

const Three = {
  functional: true,
  name: "Three",
  render: h => <h1 class="bg-purple">Three</h1>
}

@Component({
  components: {
    Settings,
    Layout,
    AwesomeHeader: Header,
    AwesomeFooter: Footer
  }
})
export default class App extends Vue {
  comps = [One, Two, Three]
  selectedComp = this.comps[0]
}
</script>

 

转载于:https://www.cnblogs.com/Answer1215/p/9373412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值