Vue router 没有 render/mount 根路径组件
Vue router does not render/mount root path component
我正在制作一个包含 vue
、vue-router
和 laravel
的页面,问题是,当我输入 localhost/myproject/public_html/
时,Home
组件是未在 router-view
中呈现,如果我在路由器 link 中单击到服务组件,它会正常呈现内容,而当我单击到主路径时,它会呈现主组件内容,所以为什么会这样发生?这是我的 app.js
结构
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import App from './views/App'
import Home from './views/Home'
import Service from './views/Service'
const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: 'servicios',
'name' : 'services',
component: Service
}
],
});
const app = new Vue({
el: '#app',
components : { App },
router,
});
这是我的App.vue
<template>
<div>
Hola
<router-link :to="{ name : 'services' }">
ir a servicios
</router-link>
<router-view>
</router-view>
</div>
</template>
<script>
import PageLogo from '../components/PageLogo'
import Loading from '../components/Loading'
export default {
mounted : function(){
console.log(this.$router.currentRoute.path)
},
components : {
'page-logo' : PageLogo,
'loading' : Loading
},
methods : {
ajaxOver : function() {
}
}
}
</script>
这是我的Home.vue
<template>
<div class="row">
Home page
<router-link :to="{ name : 'services' }">
go to services
</router-link>
</div>
</template>
<script>
export default {
mounted: function() {
console.log('hola')
}
}
</script>
这是Service.vue
<template>
<div>
Services page
<router-link :to="{ name : 'home' }">
go to home
</router-link>
</div>
</template>
<script>
export default {
mounted : function(){
console.log('services')
}
}
</script>
那我该如何解决呢?如果我在任何路由中重新加载页面,应该挂载 component
,但在 vue 路由器中未显示,因此未挂载 component
。
编辑:
根据要求,在 App.vue 中的日志是 /myproject/public_html/
我以前从未使用过 Laravel,但我认为您正在寻找 base。
Vue 文档:
- type: string
- default: "/"
The base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value "/app/".
由于您在 localhost/myproject/public_html/
服务您的项目,vue 看到 /myproject/public_html/
而不是 /
。
您可以通过将基本路由添加到 vue-router 构造函数来更改此设置。
const router = new VueRouter({
mode: 'history',
base: '/myproject/public_html/',
...
}
我正在制作一个包含 vue
、vue-router
和 laravel
的页面,问题是,当我输入 localhost/myproject/public_html/
时,Home
组件是未在 router-view
中呈现,如果我在路由器 link 中单击到服务组件,它会正常呈现内容,而当我单击到主路径时,它会呈现主组件内容,所以为什么会这样发生?这是我的 app.js
结构
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import App from './views/App'
import Home from './views/Home'
import Service from './views/Service'
const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: 'servicios',
'name' : 'services',
component: Service
}
],
});
const app = new Vue({
el: '#app',
components : { App },
router,
});
这是我的App.vue
<template>
<div>
Hola
<router-link :to="{ name : 'services' }">
ir a servicios
</router-link>
<router-view>
</router-view>
</div>
</template>
<script>
import PageLogo from '../components/PageLogo'
import Loading from '../components/Loading'
export default {
mounted : function(){
console.log(this.$router.currentRoute.path)
},
components : {
'page-logo' : PageLogo,
'loading' : Loading
},
methods : {
ajaxOver : function() {
}
}
}
</script>
这是我的Home.vue
<template>
<div class="row">
Home page
<router-link :to="{ name : 'services' }">
go to services
</router-link>
</div>
</template>
<script>
export default {
mounted: function() {
console.log('hola')
}
}
</script>
这是Service.vue
<template>
<div>
Services page
<router-link :to="{ name : 'home' }">
go to home
</router-link>
</div>
</template>
<script>
export default {
mounted : function(){
console.log('services')
}
}
</script>
那我该如何解决呢?如果我在任何路由中重新加载页面,应该挂载 component
,但在 vue 路由器中未显示,因此未挂载 component
。
编辑:
根据要求,在 App.vue 中的日志是 /myproject/public_html/
我以前从未使用过 Laravel,但我认为您正在寻找 base。
Vue 文档:
- type: string
- default: "/"
The base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value "/app/".
由于您在 localhost/myproject/public_html/
服务您的项目,vue 看到 /myproject/public_html/
而不是 /
。
您可以通过将基本路由添加到 vue-router 构造函数来更改此设置。
const router = new VueRouter({
mode: 'history',
base: '/myproject/public_html/',
...
}