首页 文章

Laravel 5.2 - vue集成

提问于
浏览
0

我有一个使用Laravel 5.2版本构建的遗留项目,我想知道如何使用它设置Vue . 我在package.json文件中添加了vue:

{
  "private": true,
  "scripts": {
    "prod": "gulp --production",
    "dev": "gulp watch"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "laravel-elixir": "^5.0.0",
    "bootstrap-sass": "^3.0.0",
    "vue": "^2.0.1"
  }
}

我在resources / assets / js文件夹中创建了 bootstrap.jsapp.js 文件:

bootstrap.js

window._ = require('lodash');

/**
 * Vue is a modern JavaScript library for building interactive web interfaces
 * using reactive data binding and reusable components. Vue's API is clean
 * and simple, leaving you to focus on building your next great project.
 */

window.Vue = require('vue');

app.js

require('./bootstrap');

/**
 * First we will load all of this project's JavaScript dependencies which
 * include Vue and Vue Resource. This gives a great starting point for
 * building robust, powerful web applications using Vue and Laravel.
 */

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the body of the page. From here, you may begin adding components to
 * the application, or feel free to tweak this setup for your needs.
 */

Vue.component('image-input', require('./components/ImageInput.vue'));

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

这是组件 ImageInput.vue

<template>
    <div>
        <div class="row">
            <div class="large-6 columns">
                <div class="Image-input__image-wrapper">
                    <i v-show="! imageSrc" class="icon fa fa-picture-o"></i>
                    <img v-show="imageSrc" class="Image-input__image" :src="imageSrc">
                    <i v-show="imageSrc" @click="removeImage" class="remove fa fa-times"></i>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="large-6 columns">
                <div class="Image-input__input-wrapper">
                    Last opp bilder
                    <input @change="previewThumbnail" class="Image-input__input" name="image" type="file">
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {

  props: ['imageSrc'],

  methods: {
    previewThumbnail: function(event) {
      var input = event.target;

      if (input.files && input.files[0]) {
        var reader = new FileReader();

        var vm = this;

        reader.onload = function(e) {
          vm.imageSrc = e.target.result;
        }

        reader.readAsDataURL(input.files[0]);
      }
    },
    removeImage: function removeImage(e) {
      this.imageSrc = '';
      document.querySelectorAll('input[type=file]').forEach(element => {
          element.value = "";
      });
      document.getElementById('oldImage').value = '';
    }
  }
}
</script>

我已经像这样设置了 gulpfile

elixir(function(mix) {
    mix.sass('app.scss')
       .browserify('app.js');
});

但是,在终端中运行gulp命令后,我收到一个错误:

gulp-notify:[Laravel Elixir] Browserify失败!:意外的令牌/home/vagrant/Projects/flight-park-backend/resources/assets/js/components/ImageInput.vue:1

对于:

<template>

1 回答

相关问题