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

标签 javascript laravel-5 vuejs2 sweetalert2

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

<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 .这应该会简化整个过程,而且它的实现会更好,因为它使用事件总线来打开/关闭,并且不再使用 $refs,根据 official Vue docs 应该避免使用 $refs .

例如在您的 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 中的简单文档有点令人困惑,而且关于插件的内容不是最新的。让我知道这是否适合您!

关于javascript - Vue.js 2- sweet alert package simplert 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48164368/

相关文章:

javascript - 是否有支持 @augments 等的 JavaScript IDE?

javascript - extjs datecolumn 显示休息一天的日期

javascript - 圆 Angular 变成矩形?

php - 需要获取任务标签在 $TagArray 中的任务

在 Laravel 中安装 Dompdf 后出现 PHP 错误

javascript - 如何在 webpack 和 Vuejs 中使用带有工具栏的完整 PDF.js 查看器?

vue.js - Vue 键不会从对象中删除

javascript - Uncaught TypeError : pre, template,textarea,script,style is not iterable

laravel-5 - SQLSTATE[HY000] : General error: 1215 Cannot add foreign key constraint Laravel

javascript - 我是否缺少 vue 路由器转换的某些内容?