javascript - Vue路由器不渲染/挂载根路径组件

标签 javascript vue.js vue-router

我正在使用vuevue-routerlaravel制作一个页面,当我输入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>

this is the first load of the page, as you can see, the home component content is not being loaded

if i click in the service link, it show the content of the service component if i click in the service component to go to home, it show the home component content

那么我该如何解决这个问题呢?如果我在任何路由中重新加载页面,则应安装组件,但在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/

相关文章:

javascript - 在 angularjs 中调用 http (RESTFUL WebAPI) 的正确方法

Javascript - 试图在动态属性中包含对象

spring - Vue.JS 和 Spring Boot - 在 404 上重定向到主页

javascript - 在 Vue.js 中使用组件数据设置 router-link 属性

javascript - 从 iframe 更改文本区域值

javascript - 循环数组内的数字 - React

vue.js - 将日期插入数据库后如何在vue js中显示成功消息

css - Webpack sass 加载程序 : How to correctly use fonts?

javascript - Vuejs 将隐藏输入的值设置为路由参数

javascript - 顶部导航菜单取决于侧边栏