Laravel - Vue.js application not working without p

2020-07-24 12:49发布

问题:

I have been trying to run my vue.js app with laravel, without

php artisan serve

But been seriously failing, here is my code:

package.json:

"axios": "^0.17",
"bootstrap-sass": "^3.3.7",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"vue": "^2.5.7",
"vue-router": "^3.0.1"

app.js:

import './bootstrap'
import router from './routes'

const app = new Vue({
    el: '#root',
    router
});

routes.js:

import VueRouter from 'vue-router'
let routes = [
    {
        path: '/',
        component: require('./components/site/layouts/main-page'),
        children: [
            {
                path: '/',
                component: require('./components/site/views/home')
            }
        ]
    }
];

export default new VueRouter({
    mode: 'history',
    linkActiveClass: 'active',
    routes
});

main-page.vue:

<template>
<div>
    <header></header>
    <router-view></router-view>
    <footer></footer>
</div>
</template>

home.vue:

<template>
<div>
    <h1>Hello World!</h1>
</div>
</template>

welcome.blade.php:

<div id="root">
     <router-view></router-view>
</div>

I shall be much obliged if anyone can assist me by correcting my wrong.

回答1:

You can not run it without php artisan serve because vue is being handeled by Laravel. If laravel is not running how you can run vue app ?.

Your first application route is in web.php which is being handeled by laravel If you dont run above command then app will not work.

Anyhow, If you want to run Vue separately then make separate Vue app with:

npm install vue then

npm install --global vue-cli

then

vue init webpack my-project

then

cd my-project

and run with npm run dev It will be totally separate Vue app.It will not require php artisan serve