mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
feat: add layout on module
This commit is contained in:
parent
8fcef54a23
commit
f0a6bdd51a
@ -71,6 +71,14 @@ module.exports = class ModuleContainer {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addLayout(template, name) {
|
||||||
|
const { dst, src } = this.addTemplate(template)
|
||||||
|
const srcPath = path.parse(src)
|
||||||
|
|
||||||
|
// Add to nuxt layouys
|
||||||
|
this.options.layouts[name || srcPath.name] = `./${dst}`
|
||||||
|
}
|
||||||
|
|
||||||
addServerMiddleware(middleware) {
|
addServerMiddleware(middleware) {
|
||||||
this.options.serverMiddleware.push(middleware)
|
this.options.serverMiddleware.push(middleware)
|
||||||
}
|
}
|
||||||
|
3
test/fixtures/module/modules/basic/index.js
vendored
3
test/fixtures/module/modules/basic/index.js
vendored
@ -8,6 +8,9 @@ module.exports = function basicModule(options, resolve) {
|
|||||||
// Add a plugin
|
// Add a plugin
|
||||||
this.addPlugin(path.resolve(__dirname, 'reverse.js'))
|
this.addPlugin(path.resolve(__dirname, 'reverse.js'))
|
||||||
|
|
||||||
|
// Add a layout
|
||||||
|
this.addLayout(path.resolve(__dirname, 'layout.vue'))
|
||||||
|
|
||||||
// Extend build
|
// Extend build
|
||||||
this.extendBuild((config, { isClient, isServer }) => {
|
this.extendBuild((config, { isClient, isServer }) => {
|
||||||
// Do nothing!
|
// Do nothing!
|
||||||
|
6
test/fixtures/module/modules/basic/layout.vue
vendored
Normal file
6
test/fixtures/module/modules/basic/layout.vue
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<h1>Module Layouts</h1>
|
||||||
|
<nuxt />
|
||||||
|
</div>
|
||||||
|
</template>
|
6
test/fixtures/module/router.js
vendored
6
test/fixtures/module/router.js
vendored
@ -5,6 +5,7 @@ Vue.use(Router)
|
|||||||
|
|
||||||
const indexPage = () => import('~/views/index.vue').then(m => m.default || m)
|
const indexPage = () => import('~/views/index.vue').then(m => m.default || m)
|
||||||
const aboutPage = () => import('~/views/about.vue').then(m => m.default || m)
|
const aboutPage = () => import('~/views/about.vue').then(m => m.default || m)
|
||||||
|
const layoutPage = () => import('~/views/layout.vue').then(m => m.default || m)
|
||||||
|
|
||||||
export function createRouter() {
|
export function createRouter() {
|
||||||
return new Router({
|
return new Router({
|
||||||
@ -19,6 +20,11 @@ export function createRouter() {
|
|||||||
path: '/about',
|
path: '/about',
|
||||||
component: aboutPage,
|
component: aboutPage,
|
||||||
name: 'about'
|
name: 'about'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/layout',
|
||||||
|
component: layoutPage,
|
||||||
|
name: 'layout'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
10
test/fixtures/module/views/layout.vue
vendored
Normal file
10
test/fixtures/module/views/layout.vue
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
Layout on module
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
layout: 'layout'
|
||||||
|
}
|
||||||
|
</script>
|
@ -47,6 +47,16 @@ test.serial('Plugin', async t => {
|
|||||||
t.true(html.includes('<h1>TXUN</h1>'), 'plugin works')
|
t.true(html.includes('<h1>TXUN</h1>'), 'plugin works')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test.serial('Layout', async t => {
|
||||||
|
t.true(
|
||||||
|
nuxt.options.layouts.layout.includes('layout'),
|
||||||
|
'layout added to config'
|
||||||
|
)
|
||||||
|
|
||||||
|
const { html } = await nuxt.renderRoute('/layout')
|
||||||
|
t.true(html.includes('<h1>Module Layouts</h1>'), 'layout works')
|
||||||
|
})
|
||||||
|
|
||||||
test.serial('Hooks', async t => {
|
test.serial('Hooks', async t => {
|
||||||
t.is(nuxt.__module_hook, 1)
|
t.is(nuxt.__module_hook, 1)
|
||||||
t.is(nuxt.__renderer_hook, 2)
|
t.is(nuxt.__renderer_hook, 2)
|
||||||
|
Loading…
Reference in New Issue
Block a user