提交 39061a49 编写于 作者: E Evan You

component import + hash jumping

上级 c7cd345f
......@@ -32,7 +32,16 @@ export function createApp () {
fallback: false,
linkActiveClass: '',
linkExactActiveClass: '',
routes
routes,
scrollBehavior: (to, from, saved) => {
if (saved) {
return saved
} else if (to.hash) {
return { selector: to.hash }
} else {
return { x: 0, y: 0 }
}
}
})
const app = new Vue({
......
......@@ -135,20 +135,16 @@ async function resolveOptions (sourceDir) {
return options
}
async function genComponentRegistrationFile ({ sourceDir, pageFiles }) {
async function genComponentRegistrationFile ({ sourceDir }) {
function genImport (file) {
const name = fileToComponentName(file)
const baseDir = /\.md$/.test(file)
? sourceDir
: path.resolve(sourceDir, '.vuepress/components')
const baseDir = path.resolve(sourceDir, '.vuepress/components')
const absolutePath = path.resolve(baseDir, file)
const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
return code
}
const components = (await resolveComponents(sourceDir)) || []
const all = [...pageFiles, ...components]
return `import Vue from 'vue'\n` + all.map(genImport).join('\n')
return `import Vue from 'vue'\n` + components.map(genImport).join('\n')
}
const indexRE = /\breadme\.md$/i
......@@ -189,12 +185,20 @@ async function resolveComponents (sourceDir) {
return await globby(['**/*.vue'], { cwd: componentDir })
}
async function genRoutesFile ({ siteData: { pages }}) {
function genRoute ({ path }) {
async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
function genRoute ({ path: pagePath }, index) {
const file = pageFiles[index]
const filePath = path.resolve(sourceDir, file)
const code = `
{
path: ${JSON.stringify(path)},
component: Theme
path: ${JSON.stringify(pagePath)},
component: Theme,
beforeEnter: (to, from, next) => {
import(${JSON.stringify(filePath)}).then(comp => {
Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default)
next()
})
}
}`
return code
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册