LoginSignup
2
1

More than 3 years have passed since last update.

Laravel7.XでVue.jsを実装してみる

Last updated at Posted at 2020-03-17

npmを起動してみる

npm run watchを叩くと以下のエラーメッセージが出力

terminal
'cross-env' is not recognized as an internal or external command,
operable program or batch file.

こちらのリンクを参考にして解決
https://stackoverflow.com/questions/45034581/laravel-5-4-cross-env-is-not-recognized-as-an-internal-or-external-command

Bootstrap-Vueを実装するための環境構築

こちらのドキュメントを参考に実施(https://bootstrap-vue.js.org/docs)

npmでbootstrap-vueをインストール
terminal
# With npm
npm install vue bootstrap-vue bootstrap
bootstrap-vueをインポート
resources\js\bootstrap.js
import Vue from 'vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
jsとBladeをVueに合わせて構成
resources\js\components\example.vue
<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">{{ greeting }}</div>
                    <div class="card-body">
                        I'm an example component.
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    module.exports = {
        data: function() {
            return {
                greeting: 'hello'
            }
        }
    }
</script>
resources\js\app.js
require('./bootstrap');

// Bootstrap-vueの実装
import Vue from 'vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

window.Vue = require('vue');
import ExampleComponent from './components/example.vue'
Vue.component('example-component', ExampleComponent)

new Vue({
    el: '#app'
});
resources\views\welcome.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="{{ mix('/css/app.css') }}">
        <title>Laravel</title>
    </head>
    <body>
        <div id="app">
            <example-component></example-component>
        </div>
    </body>
    <script src="{{ mix('/js/app.js') }}"></script>
</html>
npmとapacheを起動
npm起動
npm run watch

 DONE  Compiled successfully in 8718ms                                                                                                                                                                                                           22:31:38
       Asset      Size   Chunks             Chunk Names
/css/app.css   0 bytes  /js/app  [emitted]  /js/app
  /js/app.js  3.04 MiB  /js/app  [emitted]  /js/app
artisanコマンドでサーバー起動
php artisan serve
Laravel development server started: http://127.0.0.1:8000
結果画面確認

image.png

:relaxed::relaxed: OK :relaxed::relaxed:

Bootstrap-vueの実装

ナビゲーションバーに実装

コンポネントを作成して同じくapp.jsで定義する

resources\js\app.js
import NavVar from './components/navbar.vue'
Vue.component('navigation-bar', NavVar)

定義したコンポネントをタグで作成

resources\views\welcome.blade.php
<div id="app">
    <navigation-bar></navigation-bar>
    <example-component></example-component>
</div>
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1