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