我想利用VueJS组件来简化我的脚本。因此,我为加载到Laravel中的每个页面都有一个组件。目前为止一切都工作正常。但是我在将当前的脚本逻辑传输到组件时遇到了问题。
这是当前的脚本设置,导入要使用的组件: main.js
个别页面(在每个页面中我都使用Vue创建了一个模块)的样子如下:
这是当前的脚本设置,导入要使用的组件: 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...
这一部分。我使用谷歌地图,因此当页面加载时,它会搜索app.init方法。这是我在刀片模板中加载的脚本的一部分:
<!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创建了一个模块)的样子如下:
@extends('app', ['vueView' => 'home-view'])
@section('page_title', 'title')
@section('page_description', 'title')
@section('content')
content
@endsection
通过定义vueView
变量,使用了我在脚本中导入的正确模块。
目标是将HomeView
组件作为主谷歌地图视图,并使用其他组件来加载对应链接页面。最终,我不希望所有VueJS代码都在一个脚本中,因此需要各个模块。
当我传输当前JS文件的所有内容时,会出现错误提示app.init
不是函数。该组件如下所示:
<script>
export default {
data: {
// SEARCH
addressComponents: '',
autocompletePlace: '',
autocompleteAddress: '',
(...)
}
如何修改我的组件,以便app.init加载仍然有效?
window.app = new Vue
表达式让我头疼,因为我不知道如何正确使用组件,以便在加载 Google 地图脚本时找到正确的 init 方法。 - sesc360