mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 15:50:32 +00:00
config.router + base + linkClassActive
This commit is contained in:
parent
2cc88acbe9
commit
ea1c5dee15
@ -7,9 +7,11 @@
|
|||||||
Add your custom routes inside `nuxt.config.js`:
|
Add your custom routes inside `nuxt.config.js`:
|
||||||
```js
|
```js
|
||||||
module.exports = {
|
module.exports = {
|
||||||
routes: [
|
router: {
|
||||||
{ path: '/users/:id', component: 'pages/user' }
|
routes: [
|
||||||
]
|
{ path: '/users/:id', component: 'pages/user' }
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
routes: [
|
router: {
|
||||||
{ name: 'user', path: '/users/:id', component: 'pages/_user' }
|
routes: [
|
||||||
]
|
{ name: 'user', path: '/users/:id', component: 'pages/_user' }
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import Meta from 'vue-meta'
|
|||||||
Vue.use(Router)
|
Vue.use(Router)
|
||||||
Vue.use(Meta)
|
Vue.use(Meta)
|
||||||
|
|
||||||
<% routes.forEach(function (route) { %>
|
<% router.routes.forEach(function (route) { %>
|
||||||
const <%= route._name %> = process.BROWSER ? () => System.import('<%= route._component %>') : require('<%= route._component %>')
|
const <%= route._name %> = process.BROWSER ? () => System.import('<%= route._component %>') : require('<%= route._component %>')
|
||||||
<% }) %>
|
<% }) %>
|
||||||
|
|
||||||
@ -28,15 +28,17 @@ const scrollBehavior = (to, from, savedPosition) => {
|
|||||||
|
|
||||||
export default new Router({
|
export default new Router({
|
||||||
mode: 'history',
|
mode: 'history',
|
||||||
|
base: '<%= router.base %>',
|
||||||
|
linkActiveClass: '<%= router.linkActiveClass %>',
|
||||||
scrollBehavior,
|
scrollBehavior,
|
||||||
routes: [
|
routes: [
|
||||||
<% routes.forEach((route, i) => { %>
|
<% router.routes.forEach((route, i) => { %>
|
||||||
{
|
{
|
||||||
path: '<%= route.path %>',
|
path: '<%= route.path %>',
|
||||||
component: <%= route._name %><% if (route.name) { %>,
|
component: <%= route._name %><% if (route.name) { %>,
|
||||||
name: '<%= route.name %>'<% } %><% if (route.meta) { %>,
|
name: '<%= route.name %>'<% } %><% if (route.meta) { %>,
|
||||||
meta: <%= JSON.stringify(route.meta) %><% } %>
|
meta: <%= JSON.stringify(route.meta) %><% } %>
|
||||||
}<%= (i + 1 === routes.length ? '' : ',') %>
|
}<%= (i + 1 === router.routes.length ? '' : ',') %>
|
||||||
<% }) %>
|
<% }) %>
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
@ -9,6 +9,9 @@ import { getMatchedComponents, getContext } from './utils'
|
|||||||
const isDev = <%= isDev %>
|
const isDev = <%= isDev %>
|
||||||
const _app = new Vue(app)
|
const _app = new Vue(app)
|
||||||
|
|
||||||
|
// Fix issue from vue-router Abstract mode with base (use for server-side rendering)
|
||||||
|
router.history.base = '<%= router.base %>'
|
||||||
|
|
||||||
// This exported function will be called by `bundleRenderer`.
|
// This exported function will be called by `bundleRenderer`.
|
||||||
// This is where we perform data-prefetching to determine the
|
// This is where we perform data-prefetching to determine the
|
||||||
// state of our application before actually rendering it.
|
// state of our application before actually rendering it.
|
||||||
|
@ -93,7 +93,7 @@ module.exports = function * () {
|
|||||||
yield mkdirp(r(this.dir, '.nuxt/dist'))
|
yield mkdirp(r(this.dir, '.nuxt/dist'))
|
||||||
}
|
}
|
||||||
// Resolve custom routes component path
|
// Resolve custom routes component path
|
||||||
this.options.routes.forEach((route) => {
|
this.options.router.routes.forEach((route) => {
|
||||||
if (route.component.slice(-4) !== '.vue') {
|
if (route.component.slice(-4) !== '.vue') {
|
||||||
route.component = route.component + '.vue'
|
route.component = route.component + '.vue'
|
||||||
}
|
}
|
||||||
@ -131,7 +131,7 @@ function * generateRoutesAndFiles () {
|
|||||||
routes.push({ path: path, component: r(this.dir, file) })
|
routes.push({ path: path, component: r(this.dir, file) })
|
||||||
})
|
})
|
||||||
// Concat pages routes and custom routes in this.routes
|
// Concat pages routes and custom routes in this.routes
|
||||||
this.routes = routes.concat(this.options.routes)
|
this.routes = routes.concat(this.options.router.routes)
|
||||||
/*
|
/*
|
||||||
** Interpret and move template files to .nuxt/
|
** Interpret and move template files to .nuxt/
|
||||||
*/
|
*/
|
||||||
@ -147,6 +147,11 @@ function * generateRoutesAndFiles () {
|
|||||||
]
|
]
|
||||||
let templateVars = {
|
let templateVars = {
|
||||||
isDev: this.dev,
|
isDev: this.dev,
|
||||||
|
router: {
|
||||||
|
base: this.options.router.base,
|
||||||
|
linkActiveClass: this.options.router.linkActiveClass,
|
||||||
|
routes: this.routes
|
||||||
|
},
|
||||||
store: this.options.store,
|
store: this.options.store,
|
||||||
css: this.options.css,
|
css: this.options.css,
|
||||||
plugins: this.options.plugins.map((p) => r(this.dir, p)),
|
plugins: this.options.plugins.map((p) => r(this.dir, p)),
|
||||||
@ -154,12 +159,11 @@ function * generateRoutesAndFiles () {
|
|||||||
components: {
|
components: {
|
||||||
Loading: r(__dirname, '..', 'app', 'components', 'Loading.vue'),
|
Loading: r(__dirname, '..', 'app', 'components', 'Loading.vue'),
|
||||||
ErrorPage: r(__dirname, '..', '..', 'pages', (this.dev ? '_error-debug.vue' : '_error.vue'))
|
ErrorPage: r(__dirname, '..', '..', 'pages', (this.dev ? '_error-debug.vue' : '_error.vue'))
|
||||||
},
|
}
|
||||||
routes: this.routes
|
|
||||||
}
|
}
|
||||||
// Format routes for the lib/app/router.js template
|
// Format routes for the lib/app/router.js template
|
||||||
// TODO: check .children
|
// TODO: check .children
|
||||||
templateVars.routes.forEach((route) => {
|
templateVars.router.routes.forEach((route) => {
|
||||||
route._component = route.component
|
route._component = route.component
|
||||||
route._name = '_' + hash(route._component)
|
route._name = '_' + hash(route._component)
|
||||||
route.component = route._name
|
route.component = route._name
|
||||||
|
@ -19,7 +19,7 @@ module.exports = function () {
|
|||||||
vendor: ['vue', 'vue-router', 'vue-meta', 'es6-promise', 'es6-object-assign']
|
vendor: ['vue', 'vue-router', 'vue-meta', 'es6-promise', 'es6-object-assign']
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
publicPath: '/_nuxt/'
|
publicPath: join(this.options.router.base, '/_nuxt/')
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
modules: [
|
modules: [
|
||||||
|
17
lib/nuxt.js
17
lib/nuxt.js
@ -19,7 +19,6 @@ class Nuxt {
|
|||||||
constructor (options = {}, cb) {
|
constructor (options = {}, cb) {
|
||||||
var defaults = {
|
var defaults = {
|
||||||
dev: true,
|
dev: true,
|
||||||
routes: [],
|
|
||||||
plugins: [],
|
plugins: [],
|
||||||
css: [],
|
css: [],
|
||||||
store: false,
|
store: false,
|
||||||
@ -29,6 +28,16 @@ class Nuxt {
|
|||||||
failedColor: 'red',
|
failedColor: 'red',
|
||||||
height: '2px',
|
height: '2px',
|
||||||
duration: 5000
|
duration: 5000
|
||||||
|
},
|
||||||
|
router: {
|
||||||
|
base: '/',
|
||||||
|
linkActiveClass: 'router-link-active',
|
||||||
|
routes: []
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
filenames: {
|
||||||
|
css: 'style.css'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options.loading === true) delete options.loading
|
if (options.loading === true) delete options.loading
|
||||||
@ -127,9 +136,9 @@ class Nuxt {
|
|||||||
APP: app,
|
APP: app,
|
||||||
context: context,
|
context: context,
|
||||||
files: {
|
files: {
|
||||||
css: join('/_nuxt/', self.options.build.filenames.css),
|
css: join(self.options.router.base, '/_nuxt/', self.options.build.filenames.css),
|
||||||
vendor: join('/_nuxt/', self.options.build.filenames.vendor),
|
vendor: join(self.options.router.base, '/_nuxt/', self.options.build.filenames.vendor),
|
||||||
app: join('/_nuxt/', self.options.build.filenames.app)
|
app: join(self.options.router.base, '/_nuxt/', self.options.build.filenames.app)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return { html, error: context.nuxt.error }
|
return { html, error: context.nuxt.error }
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nuxt",
|
"name": "nuxt",
|
||||||
"version": "0.3.3",
|
"version": "0.3.4",
|
||||||
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
|
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
Loading…
Reference in New Issue
Block a user