简体   繁体   中英

vue.js import a component dynamically

I am trying to import a component dynamically, but all my component and vuex variables seem to be undefined.

function loadChart(chartName){
    let chart = chartName + '.vue'
    return System.import('@/components/charts/' + chart)
  }

  export default {
    name: "SingleChart",
    data() {
      return {
        chartTitle: this.$store.getters.getSingleChartTitle,
        chartName: this.$store.getters.getSingleChartName
      }
    },
    methods: {},
    computed: {},
    watch: {},
    props: [],
    components: {
        Chart: () => loadChart(this.chartName)
    }
  }

I get the error

Reason: Error: Cannot find module './undefined.vue'.

Based on the Vuex state page , you may not have injected the store to use it as you've referenced (this.$store...)

Vuex provides a mechanism to "inject" the store into all child components from the root component with the store option (enabled by Vue.use(Vuex)):

const app = new Vue({
    el: '#app',
    // provide the store using the "store" option.
    // this will inject the store instance to all child components.
    store,
    components: { Counter },
    template: `
      <div class="app">
        <counter></counter>
      </div>
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM