簡體   English   中英

Vue路由器不呈現/掛載根路徑組件

[英]Vue router does not render/mount root path component

我正在用vuevue-routerlaravel制作一個頁面,問題是,當我在localhost/myproject/public_html/輸入時,如果我單擊指向服務的路由器鏈接,則無法在router-view呈現Home組件。組件,它可以正常顯示內容,而當我單擊主頁路徑時,它可以顯示家用組件內容,那么為什么會發生這種情況? 這是我的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>

如您所見,這是頁面的第一次加載,home組件內容未加載

如果我單擊服務鏈接,它將顯示服務組件的內容 如果我單擊服務組件轉到主頁,則會顯示主頁組件的內容

那么我該如何解決呢? 如果我以任何路線重新加載頁面,則應該安裝該component ,但是不會在vue路由器中顯示該component ,因此不會安裝該component

編輯:

根據要求,在App.vue中,日志為/myproject/public_html/

我以前從未使用過Laravel,但我認為您正在尋找base

Vue文檔:

  • 類型:字符串
  • 默認值:“ /”

    應用程序的基本URL。 例如,如果整個單頁應用程序在/ app /下提供,則base應使用值“ / app /”。

由於您要在localhost/myproject/public_html/服務項目,因此vue看到的是/myproject/public_html/而不是/

您可以通過將基本路由添加到vue-router構造函數來更改此設置。

const router = new VueRouter({
    mode: 'history',
    base: '/myproject/public_html/',
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM