我想利用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还能用吗?

推荐答案

有人已经提到了npm上可用的GuillaumeLeclerc/vue-google-maps作为vue-google-maps,但请注意,该版本仅适用于vue 1.十、

如果你使用的是v2,看看这个很棒的vue2 fork xkjyeah/vue-google-maps——它在npm上以vue2-google-maps的形式提供.

该API非常简单,与v1版本相差不远,而且存储库比其团队对应版本更活跃.

这真的让我的vue map 工作起来比我自己的要轻松得多,这正是我最初所做的.

我希望这能有所帮助

Vue.js相关问答推荐

Vuetify从同一方向旋转传送带下一个项目和上一个项目

将 html 文件移出 dist vite 中的 src 文件夹

从组件中检索 Shopware CMS 编辑器中的自定义实体数据

为什么 Vue 在将设置脚本中的计算(computed)属性渲染到模板时找不到变量

vuetify/mdi 不显示图标

如何在 VUE.js 中重新刷新时保持多层 b-collapse 菜单打开?

在 VueJS 中为 Get、Post、Patch 配置全局标头的最佳方法

禁用 vue-cli webpack 编码图像 base64

将props传递给设置时的VueJS 3组合API和TypeScript类型问题:类型上不存在属性 user用户

在 jest document.querySelector 中测试 vue 组件期间始终返回 null

vue js 组件中的 this.$route 只返回 undefined

如何在 Vue.js 中使用 MaterializeCss?

如何为 Vue 项目设置 lint-staged?

如何像 React 中的 {...props} 一样在 Vue 中解构 props?

如何从 .vue 文件中导出多个对象

NuxtServerInit 在 Vuex 模块模式下不起作用 - Nuxt.js

我可以在 Vue 实例方法中传播的 mapMutations 中使用this吗?

如何在 vuetify 中将工具提示添加到数据表标题?

在~/components/AddPlaceModal.vue中找不到导出AddPlaceModal

居中 v-toolbar-title