我想利用VueJS组件来清理我的脚本.因此,我在Laravel中加载的每个页面都有一个组件.到目前为止一切正常.但我在将当前脚本逻辑转移到组件时遇到了问题.
这是导入要使用的组件的当前脚本设置:
main.js
import HomeView from './components/HomeView.vue';
import IncidentView from './components/IncidentView.vue';
window.app = new Vue({
el: '.content',
components: {
HomeView, IncidentView
},
methods: {
// Init GIS
init: function() {
// Initialize GIS Map
initGISMap(this.$els.map);
},
}
(...)
}
对我来说,关键是window.app = new Vue...
部分.我使用谷歌 map ,因此当页面加载时,它会搜索应用程序.初始化方法.这是我在blade模板中加载的脚本的一部分:
<!DOCTYPE html>
<html>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
<h1>
@yield('page_title')
<small>@yield('page_description')</small>
</h1>
</section>
<!-- Main content -->
<section class="content">
@if (isset($vueView))
<component is="{{ $vueView }}">
@endif
@yield('content')
</component>
</section>
<!-- /.content -->
<script src="https://maps.googleapis.com/maps/api/js?key=KEY&libraries=places&callback=app.init" async defer></script>
</body>
</html>
各个页面(我在Vue中 for each 模块创建的页面)如下所示:
@extends('app', ['vueView' => 'home-view'])
@section('page_title', 'title')
@section('page_description', 'title')
@section('content')
content
@endsection
通过定义vueView
变量,将使用我在脚本中导入的正确模块.
目标是使用HomeView
组件作为谷歌 map 的主视图.以及我在点击主题中相应链接时加载的不同页面的其他组件.最后,我不想将所有VueJS代码都放在一个脚本中.因此,模型.
当我传输当前JS文件的所有内容时,我收到一个错误,抱怨app.init
不是一个函数.组件如下所示:
<script>
export default {
data: {
// SEARCH
addressComponents: '',
autocompletePlace: '',
autocompleteAddress: '',
(...)
}
我该如何修改我的组件,使之与应用程序兼容.init load还能用吗?