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

component import + hash jumping

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