按需加载子组件

这个博客内容展示了如何在Vue应用中实现组件的动态加载,通过监听`navInfo`的变化来更新左侧栏的组件。在`activated()`钩子中处理页面激活状态,并在`watch`中平滑切换组件的动画效果。同时,利用事件总线(eventBus)接收底部导航传来的参数,实现实时更新组件内容。整个过程涉及Vue组件、状态管理和动态导入。
摘要由CSDN通过智能技术生成

在这里插入图片描述

  <div>
    <div>
      <div class="content">
        <div style="z-index: 999">
          <div :class="['left', inAnimation ? 'Animationleft' : '']" ref="left">
            <component :is="currentLeft"></component>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script>
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import eventBus from '@/utils/vueBus'
@Component({
  components: {},
})
export default class Pagecontent extends Vue {
  navInfo = {}
  inAnimation = false
  id = 0
  componentleft = [
    { name: 'firstPage', component: 'indexLeft' },
    { name: 'govern', component: 'governLeft' },
    { name: 'serve', component: 'serveLeft' },
    { name: 'health', component: 'healthLeft' },
    { name: 'education', component: 'educationLeft' },
    { name: 'LC', component: 'lcLeft' },
    { name: 'traffic', component: 'trafficLeft' },
    { name: 'neighbours', component: 'neighboursLeft' },
    { name: 'business', component: 'businessLeft' },
    { name: 'architect', component: 'architectLeft' },
  ]
  activated() {}
  @Watch('navInfo')
  onChangeValue(newVal, oldVal) {
    this.inAnimation = false
    setTimeout(() => {
      this.inAnimation = true
    }, 200)
  }
  get currentLeft() {
    Vue.component(
      this.componentleft[this.id - 1] &&
        this.componentleft[this.id - 1].component,
      () =>
        import(
          `@/components/scene/${this.componentleft[this.id - 1].name}/${
            this.componentleft[this.id - 1].component
          }.vue`
        )
    )
    return this.componentleft[this.id - 1].component
  }
  async created() {
    this.inAnimation = false
    setTimeout(() => {
      this.inAnimation = true
    }, 200)
    //底部导航穿过来的参数
    eventBus.$on('navValue', (data) => {
      this.navInfo = data
      this.id = data.id
    })
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值