Vue.js 2- sweet alert package simplert 不工作

Vue.js 2- sweet alert package simplert not working

我想在我使用 Laravel 5.2 版本构建的应用程序中使用这个 library 作为警报。我已经安装了它并创建了一个这样的组件:

<script>
import Simplert from 'vue2-simplert'

export default {
  data () {
    return {
      obj: {
        title: 'Alert Title',
        message: 'Alert Message',
        type: 'info',
        useConfirmBtn: true,
        customConfirmBtnText: 'OK'
      },
    }
  },
  methods: {
    openSimplert () {
      this.$refs.simplert.openSimplert(this.obj)
    },
  }
}
</script>

我正在 app.js 中注册组件,如下所示:

Vue.component('alert', require('./components/Alert.vue'));

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

然后尝试在我的模板中使用它:

<alert :useRadius="true"
       :useIcon="true"
       ref="simplert">
</alert>

它是此模板的一部分:

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <a class="btn btn-info link-button" href="/extras/create" role="button">Lag ny extra</a>
            <div class="panel panel-default">
                <div class="panel-heading">Extras</div>
                <div class="panel-body">
                    @foreach($data as $extra)
                      <div class="media row">
                        <div class="media-left col-sm-3">
                          <a href="#">
                            <img class="media-object" src="/img/extras/{{ $extra->image_path }}" alt="{{ $extra->title }}">
                          </a>
                        </div>
                        <div class="media-body col-sm-6">
                          <h4 class="media-heading">{{ $extra->title }}</h4>
                          <p>{{ $extra->description }}</p>
                        </div>
                        <div class="col-sm-3 action-buttons">
                          <a class="btn btn-info" href="/extras/create" role="button">Rediger</a>
                          <alert :useRadius="true"
                                 :useIcon="true"
                                 ref="simplert">
                         </alert>
                        </div>
                      </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

它像这样包含在应用程序模板中:

<div id="app">
    <nav class="navbar navbar-default navbar-static-top">
     ...
    </nav>

    @yield('content')
</div>

我可以在vue调试工具中看到组件,但是没有html正在创建,我只能看到这个:

<!--function (a, b, c, d) { return createElement(vm, a, b, c, d, true); }-->

Ant 我在控制台中收到错误:

[Vue warn]: Failed to mount component: template or render function not defined.

found in

--->

更新

在使用 Laravel 5.5 创建新项目后,因为我认为 Laravel 5.2 中的设置会产生问题,所以我添加了相同的库和组件,但该组件仍然抛出错误,幸运的是其他组件现在可以工作,但这仍然会出现相同的错误,默认设置为 Laravel 5.5。

刚刚发现simplert also exists as Vue plugin. That should simplify the complete process and it is much better implemented as it uses an event bus for opening/closing and it doesn't use any longer $refs which should be avoided according to the official Vue docs

然后你会在 app.js:

import Simplert from 'vue2-simplert-plugin'
Vue.use(Simplert)

const app = new Vue({
  el: '#app',
  data: {
    obj: {
      title: 'Alert Title',
      message: 'Alert Message',
      type: 'info',
      useConfirmBtn: true,
      customConfirmBtnText: 'OK'
    }
  },
  methods: {
    openSimplert () {
      this.$Simplert.open(this.obj)
    },
    closeSimplert () {
      this.$Simplert.close()
    }
  }
})

在你的 Larvavel 模板中 只需使用:

@section('content')
  // ...
    <simplert></simplert>
  // ...
@endsection  

wiki 中的 simplert 文档有点混乱,而且关于插件不是最新的。让我知道这是否适合您!