简体   繁体   English

Laravel VueJs:`router-view` 不渲染组件

[英]Laravel VueJs : `router-view` does not render the component

I know such questions are out there in this site but they do not solve my problem.我知道这个网站上有这样的问题,但它们不能解决我的问题。 Hence this question pops up here :因此这个问题在这里弹出:

In my Laravel 5.3 and VueJs app, the root instance of Vue in app.js file points to App.vue and in App.vue I have the router-view placeholder.在我的 Laravel 5.3 和 VueJs 应用程序中, app.js文件中 Vue 的根实例指向App.vue而在App.vue我有router-view占位符。 So I expect page components to be rendered inside the placeholder but that does not happen.所以我希望页面组件在占位符内呈现,但这不会发生。

Let me show the contents in different files:让我显示不同文件中的内容:

In web.php I have :web.php我有:

Route::get('/listing/{listing}', function(Listing $listing){

    $model = $listing->toArray();

    return view('app',['model' => $model]);


}); 

In router.js , I have :router.js ,我有:

import Vue from 'vue';
import VueRouter from 'vue-router';
import ListingPage from '../components/ListingPage';

Vue.use(VueRouter);


    export default new VueRouter({
        mode:'history',
        routes:[

            { path: '/listing/:listing', component: ListingPage, name: 'listing' }
        ]

    });

In app.js I have :app.js我有:

import ListingPage from '../components/ListingPage.vue';
import  router  from './router';
import App from '../components/App.vue';

var app = new Vue({

    el: '#app',
    render: h => h(App),
    router

});

So when I hit the url /listing/5 , the application goes to App.vue and is supposed to render the ListingPage component inside router-view placeholder.所以,当我打的网址/listing/5 ,申请去App.vue并应该使ListingPage内部组件router-view占位符。

In App.vue , I have :App.vue ,我有:

<template>
    <div>

        <div id="toolbar">
            <router-link :to="{ name: 'home' }">
                <img class="icon" src="/images/logo.png">
                <h1>vuebnb</h1>
            </router-link>
        </div>
        <router-view></router-view>
    </div>
</template>

In ListingPage.vue , I have :ListingPage.vue ,我有:

  <template>
        <div>

            <div class="container">
                <div class="heading">
                    Heading from listing page 
                </div>
                <hr>
                <div class="about">
                    <h3>About this listing page</h3>

                </div>
            </div>
    </template>

<script>

export default {

           data() {
               return {
                   index: 1
                     }
                  }
          }
</script>

But finally I only get the content in App.vue without the ListingPage component being rendered inside the placeholder.但最后我只得到在内容App.vue没有ListingPage组件所呈现的占位符内。

How can I get the proper rendering there ?我怎样才能在那里获得正确的渲染?

EDIT:编辑:

Actually this questions arises out of the source code of the book ' Full-Stack Vue.js 2 and Laravel 5 ' by Anthony Gore.实际上,这个问题出自 Anthony Gore 所著的《 全栈 Vue.js 2 和 Laravel 5 》一书的源代码。 The source code is available at github here .源代码可在此处的github 上获得。 I tried to run the codebase at Chapter07.我试图在 Chapter07 运行代码库。 Of course I ran the necessary commands such as composer install, npm install, npm run dev in localhost with Laravel 5.5.6 version but finally when I hit any URL like /listing/5 , I do not see any content rendered from ListingPage component.当然,我使用Laravel 5.5.6 版本localhost运行了必要的命令,例如composer install, npm install, npm run dev ,但最后当我点击/listing/5类的任何 URL 时,我没有看到从ListingPage组件呈现的任何内容。

You can take the code from github or you may download it from here to run in your localhost.您可以从 github 获取代码,也可以从此处下载以在本地主机中运行。

What might be the reason that it does not work and the potential solution as well ?它不起作用的原因可能是什么以及潜在的解决方案?

You need to declare your base path in touter config like this :您需要像这样在 touter 配置中声明您的基本路径:

export default new VueRouter({
  mode:'history',
  routes:[

    { path: '/:listing', component: ListingPage, name: 'listing' }
  ],
  base:'/listing',
  root:'/',

});

I think that inside of "render: h => h(App)" you can use我认为在"render: h => h(App)"你可以使用

components: {App},
template: '<App></App>'

Then the app.js code will be like:然后 app.js 代码将如下所示:

import  router  from './router';
import App from '../components/App.vue';

var app = new Vue({
    el: '#app',
    components: {App},
    template: '<App></App>',
    router
});

I hope that works.我希望这有效。

best regards!此致!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM