1
我試圖建立一個vuejs應用程序,將有成百上千的「形式」,或者說都得到動態<component is=''>
標籤內換出的「頁面」組件。問題是必須導入每個組件。有沒有辦法根據路由參數動態導入組件?到目前爲止,我有以下,但它不工作:Vuejs大型應用延遲加載動態組件
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const Hello = resolve => require(['@/components/Hello.vue'], resolve)
export default new Router({
routes: [{
path: '/',
name: 'Hello',
component: Hello
}, {
path: '/:name',
name: 'Dynamic',
component : {
template: '<component :is="$route.params.name"></component>',
watch: {
'$route': function(to) {
window[to.params.name] = resolve => require(['@/components/' + to.params.name + '.vue'], resolve)
Vue.component(to.params.name, window[to.params.name])
console.log(to.params.name)
}
}
}
}]
})
也許這會幫忙嗎? https://gist.github.com/Rich-Harris/ea561810900eedd2a8e9afbc78ddd566 參見SystemJS https://github.com/systemjs/systemjs –