Laravel:将数据从刀片模板动态传递到Vue组件

时间:2019-08-02 04:19:15

标签: laravel vue.js

我正在尝试将动态数据从刀片模板传递到Vue Component,但没有成功,也没有错误,

以下是我的代码,任何人都可以请教一下问题,我做错了吗?

app.js

require('./bootstrap');
window.Vue = require('vue');

Vue.component('slider', require('./components/HomeSlider.vue').default);

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

HomeSlider.vue

<template>
 <slick ref="slick" class="home-slider" :options="slickOptions">
        <div class="image" v-for="slide in slides" :key="slide.id">
            <a href="">
                <img :src="slide.src" alt="option image" />
            </a>
        </div>
    </slick>
</template>
<script>
 import '../../../node_modules/slick-carousel/slick/slick.css';
    import '../../../node_modules/slick-carousel/slick/slick-theme.css';
    import '../../sass/slick.scss';
    import Slick from 'vue-slick';
    export default {
        components: {
            Slick
        },
        props: ['slides'],
        data() {
            return {
                slickOptions: {
                    autoplay: 1
                }
            };
        },
        mounted() {
            // console.log(this.slides)
        }
    }
</script>

home.blade.php

@extends('layouts.app')
@section('content')
 <slider v-bind="slides"></slider>
@endsection

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller {
    /**
        * Create a new controller instance.
        *
        * @return void
    */
    public function __construct() {
    }

    /**
        * Show the application dashboard.
        *
        * @return \Illuminate\Contracts\Support\Renderable
    */
    public function index() {
        $slides = [
            ['id' => 1, 'image' => '1.jpg'],
            ['id' => 2, 'image' => '2.jpg'],
            ['id' => 3, 'image' => '3.jpg'],
        ];
        return view('home', compact('slides'));
    }
}

1 个答案:

答案 0 :(得分:1)

尝试

home.blade.php

@extends('layouts.app')
@section('content')
 <slider :slides="{{ json_encode($slides) }}"></slider>
@endsection

现在滑杆道具中的slides方面