vuejs2多页面未加载路由器视图
我想为多页面应用程序创建一个 POC。 我通过执行 vue create poc
开始一个基本应用程序
,然后添加第二个名为 test.js 的 main.js,其行数与 main.js 相同。
// main.js
import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
// test.js
import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
// vue.config.js
module.exports = {
// https://cli.vuejs.org/config/#pages
pages: {
main: {
// entry for the page
entry: 'src/main.js',
// the source template
template: 'public/index.html',
// output as dist/index.html
//filename: 'index.html',
// when using title option,
// template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
title: 'Multi-page Main',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
//chunks: ['chunk-vendors', 'chunk-common', 'index']
},
pasquier: {
// entry for the page
entry: 'src/test.js',
// the source template
template: 'public/index.html',
// output as dist/index.html
//filename: 'index.html',
// when using title option,
// template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
title: 'Multi-page test',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
//chunks: ['chunk-vendors', 'chunk-common', 'index']
},
// when using the entry-only string format,
// template is inferred to be `public/subpage.html`
// and falls back to `public/index.html` if not found.
// Output filename is inferred to be `subpage.html`.
//subpage: 'src/subpage/main.js'
}
}
问题是,当我使用以下命令打开我的网站时网址:localhost:8080/main
我有 :
我与 localhost:8080/main 具有完全相同的页面,
这意味着 router-view 没有加载,但是 router-link 加载了。 我没有更改任何其他代码。
// App.vue
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
// router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
使用多页面时如何对根页面/
进行充电?
I want to create a POC for multi-page app.
I start with a basic app by doing vue create poc
then i add a second main.js named test.js with the same lines than the main.js
// main.js
import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
// test.js
import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
// vue.config.js
module.exports = {
// https://cli.vuejs.org/config/#pages
pages: {
main: {
// entry for the page
entry: 'src/main.js',
// the source template
template: 'public/index.html',
// output as dist/index.html
//filename: 'index.html',
// when using title option,
// template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
title: 'Multi-page Main',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
//chunks: ['chunk-vendors', 'chunk-common', 'index']
},
pasquier: {
// entry for the page
entry: 'src/test.js',
// the source template
template: 'public/index.html',
// output as dist/index.html
//filename: 'index.html',
// when using title option,
// template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
title: 'Multi-page test',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
//chunks: ['chunk-vendors', 'chunk-common', 'index']
},
// when using the entry-only string format,
// template is inferred to be `public/subpage.html`
// and falls back to `public/index.html` if not found.
// Output filename is inferred to be `subpage.html`.
//subpage: 'src/subpage/main.js'
}
}
The issue is that when I open my website with the url : localhost:8080/main
I have :
I have the same exact page with localhost:8080/main
But when I load the app without the multi-page I have:
Which mean that the router-view is not loaded, but the router-link is.
I didn't change any other code.
// App.vue
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
// router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
How can i charge the root page /
when using the multi-page ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将路由器模式从历史模式更改为哈希模式
Try to change your router mode from history to hash