我正在使用vue
、vue-router
和laravel
制作一个页面,当我输入localhost/时出现问题myproject/public_html/
,Home
组件不会在 router-view
中呈现,如果我单击指向 Service 组件的路由器链接,它会呈现正常内容,当我单击主路径时,它会呈现主组件内容,那么为什么会发生这种情况?这是我的 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>
那么我该如何解决这个问题呢?如果我在任何路由中重新加载页面,则应安装组件
,但在vue路由器中未显示,因此未安装组件
。
编辑:
根据要求,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/',
...
}
关于javascript - Vue路由器不渲染/挂载根路径组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391035/