vue通過多種方式可以將組件掛載到一個頁面上。掛載方式有四種。其實也並不止四種。這里呢就簡單的提四種方式去怎樣掛載組件。
第一種就是作為標簽形式掛載。前面也提到。
后面的就是一般的掛載組件和按需掛載組件懶加載掛載組件。用的比較多的就是后面的兩種。
1.懶加載模式下的組件加載:
1)第一步還是先創建vue組件。可以創建在views和components中,都可以。不要求嚴格。
2)創建完成后就配置路由。
法一:
const routes = [
{
path: '/',
name: 'top',
component: () => import('../components/common/top')
}
]
法二:
Vue.use(VueRouter)
const top = r => require.ensure([], () => r(require('../components/common/top')), 'top')
const routes = [
{
path: '/',
component: top
}
]
法三:
import Top from '../components/common/top'
Vue.use(VueRouter)
const routes = [
{
path: '/',
component: Top
}
]
上面這幾種方式一個是從component,一個用const,一個在頭部引入組件。
在主入口文件中只需要一個
<router-view></router-view>
標簽就即刻。
更多的東西以后更新