這裏有一份Vue動態組件解決方案,你想看嗎?

千言萬語敵不過一張動圖 git

動態組件.gif
都是有身份的程序猿,以碼會友:

const DynamicComponent = {
  props: {
    componentName: {
      type: String,
      required: true
    },
    componentProps: {
      type: Object,
      default: () => {
      }
    },
    styles: {
      type: Object,
      default: () => {
      }
    }
  },
  render(createElem) {
    return createElem(this.componentName, {
      props: this.componentProps
    })
  }
}
export default DynamicComponent;

複製代碼

使用的示例:github

<DynamicComponent v-for="(item, index) in componentsArray"
                        :key="index"
                        :componentName="item.component"
                        :componentProps="item.props"></DynamicComponent>
複製代碼

就醬子。動態的往數據裏面放數據便可bash

\color{red}{注意:須要動態加載的組件都須要使用 Vue.component() 註冊,否則會報組件沒有註冊的錯誤!}

github.com/GaoGersy/Dy…微信

點個贊再走吧ui

這篇文章若是能拿下100個喜歡我就去買一塊榮耀Magic手錶獎勵本身,說到作到!您的贊是我爲國家GDP作貢獻的原動力!this

關注微信公衆號有驚喜喲,關注就送Vue從入門到放棄全套視頻教程spa

關注就送Vue從入門到放棄全套視頻教程
相關文章
相關標籤/搜索