【问题标题】:Rendering VueJS components into a Google Map Infowindow将 VueJS 组件渲染到 Google Map Infowindow 中
【发布时间】:2018-04-29 16:41:51
【问题描述】:

我正在尝试渲染一个简单的 vue js 组件 -

var infowindow_content = "<google-map-infowindow ";
infowindow_content += "content='Hello World'";
infowindow_content += "></google-map-infowindow>";

通过将其传递到标记的信息窗口

this.current_infowindow = new google.maps.InfoWindow({
    content: infowindow_content,
});
this.current_infowindow.open(context.mapObject, marker);

vueJS 组件是 -

<template>
    <div>
        {{content}}
    </div>
</template>

<script>
module.exports = {
    name: 'google-map-infowindow',
    props: [ 
        'content',
    ],
}
</script>

但是,这不起作用,窗口是空白的。

【问题讨论】:

    标签: javascript google-maps vue.js vuejs2


    【解决方案1】:

    今天重温这个之后,我能够通过编程创建 vue 组件的实例并在简单地将其呈现的 HTML 模板作为信息窗口的内容传递之前安装它。

    InfoWindow.vue

    <template>
        <div>
            {{content}}
        </div>
    </template>
    
    <script>
    module.exports = {
        name: 'infowindow',
        props: [ 
            'content',
        ],
    }
    </script>
    

    在打开信息窗口之前需要创建的代码部分:

    ...
    import InfoWindowComponent from './InfoWindow.vue';
    ...
    
    var InfoWindow = Vue.extend(InfoWindowComponent);
    var instance = new InfoWindow({
        propsData: {
            content: "This displays as info-window content!"
        }
    });
    
    instance.$mount();
    
    var new_infowindow = new google.maps.InfoWindow({
        content: instance.$el,
    });
    
    new_infowindow.open(<map object>, <marker>);
    

    注意:我还没有尝试过观察者和事件驱动的调用。

    【讨论】:

      【解决方案2】:

      在 vue.js 中,带有“双胡须”的模板插值将其内容解释为纯文本,而不是 HTML(因此对其进行转义)。如果你想发出 HTML,你需要使用 v-html 指令:

      <div v-html="content"></div>
      

      有关参考,请参阅指南中的 Raw HTMLv-html API 文档。

      请注意,这种方式不考虑数据绑定,因此我不确定操作原始 HTML 是否是这里的最佳策略。

      【讨论】:

      • content 在示例中虽然只是一个字符串(或纯文本)。
      • 我以为你通过content 属性将&lt;google-map-infowindow&gt;...&lt;/google-map-infowindow&gt; HTML 传递给了vue 组件。如果不是,vue组件和&lt;google-map-infowindow&gt;/this.current_infowindow有什么关系?
      • 所以,&lt;google-map-infowindow&gt; 是组件的标记名。我将其作为信息窗口的 html 内容发送,而不是发送给组件本身,因为这没有任何意义。 this.current_infowindow 只不过是一个对象范围的变量,用于存储对 infowindow 对象的引用。
      • 这对我来说没有多大意义,你正在创建一个 vue &lt;google-map-infowindow&gt; 组件,但是你通过将原始 html 传递给 google.maps.InfoWindow 来在 vue 之外创建它...也许你由无渲染的 Vue 组件(没有模板且自身不渲染 HTML 的组件)更好地提供服务?在这种情况下,渲染将由谷歌库专门完成。
      猜你喜欢
      • 2020-09-05
      • 2018-12-25
      • 2019-12-09
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2021-03-16
      • 2018-10-01
      相关资源
      最近更新 更多