【问题标题】:Vue.js 2- sweet alert package simplert not workingVue.js 2-sweet alert package simplet not working
【发布时间】:2018-06-18 06:18:05
【问题描述】:

我想在我使用 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 警告]:无法挂载组件:模板或渲染函数没有 已定义。

发现于

--->

更新

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

【问题讨论】:

  • 您能否用您的第一个和第三个代码块所在的信息更新您的问题?或者也许发布他们的整个文件?
  • 我已经更新了问题
  • 我猜这是因为&lt;template&gt;&lt;/template&gt; 缺少Alert.vue
  • 但是,就像我在对 package.json 和 gulpfile 进行更改后提到的那样,即使具有模板的组件也无法正常工作
  • 使用最简单的组件的方式似乎不对。尝试删除Alert.vue 并使用它来设置警报组件Vue.component('alert', require('vue2-simplert'));。直接使用组件,在调用组件的地方做this.$refs.simplert.openSimplert(obj)

标签: javascript laravel-5.2 vuejs2 sweetalert2


【解决方案1】:

刚刚发现simplert also exists as Vue plugin。这应该会简化整个过程,并且更好地实现,因为它使用事件总线进行打开/关闭,并且不再使用$refs,根据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 中更简单的文档有点令人困惑,并且关于插件不是最新的。让我知道这是否适合你!

【讨论】:

  • 当我使用上述内容时,我收到警告[Vue warn]: Unknown custom element: &lt;simplert&gt; - did you register the component correctly? For recursive components, make sure to provide the "name" option.
  • 也许你没有正确安装?你导入然后做 Vue.use(Simplert) 吗?
  • 是的,但很可能我做错了什么,因为这是第一次使用 Vue。我在这里创建了一个包含文件内容的问题:stackoverflow.com/questions/54440029/…
猜你喜欢
  • 2017-12-06
  • 1970-01-01
  • 2018-12-04
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多