Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS components

I want to make use of VueJS components to clean up my scripts. Therefore I have a component for each page I am loading in into Laravel. Everything works fine so far. But I have issues transfering my current script logic to a component.

This is the current script setup importing the to be used components:

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);
      },
    }
(...)
}

Key for me is the window.app = new Vue... part. I make use of google maps and therefore when the page is loaded it searches for an app.init method. This is part of the script I am loading within the blade template:

<!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>

The individual pages (where I create for each a module in Vue) look like this:

@extends('app', ['vueView' => 'home-view'])
@section('page_title', 'title')
@section('page_description', 'title')

@section('content')
content
@endsection

By defining the vueView variable, the correct module I am importing in my script is used.

The goal is to use HomeView component as the main google maps view. And the other components for different pages I load when clicking the corresponding link in my theme. At the end, I do not want to have all VueJS code in one script. Therefore the models.

When I transfer all the content of this current JS file, I get an error complaining that app.init is not a function. The component looks like this:

<script>
export default {
   data: {
       // SEARCH
       addressComponents: '',
       autocompletePlace: '',
       autocompleteAddress: '',
(...)
}

How do I modify my component in a way, that the app.init load would still work?

like image 587
sesc360 Avatar asked May 10 '16 15:05

sesc360


People also ask

What are components in VueJS?

Components are reusable Vue instances with custom HTML elements. Components can be reused as many times as you want or used in another component, making it a child component. Data, computed, watch, and methods can be used in a Vue component. A webpage is made up of different components.

Does Vue have component?

Vue components are written as a combination of JavaScript objects that manage the app's data and an HTML-based template syntax that maps to the underlying DOM structure.

What are the 3 parts of a component in Vue?

Components in Vue are composed of three parts; a template (which is like HTML), styles and JavaScript. These can be split into multiple files or the same .

What is the purpose of components in VueJS?

Vue Components are one of the important features of VueJS that creates custom elements, which can be reused in HTML. Let's work with an example and create a component, that will give a better understanding on how components work with VueJS.


2 Answers

Someone has already mentioned GuillaumeLeclerc/vue-google-maps which is available on npm as vue-google-maps, but be warned that that version only works with vue 1.x

If you are using v2, look at this great vue2 fork xkjyeah/vue-google-maps - it's available as vue2-google-maps on npm.

The API is straightforward and doesn't diverge far from the v1 version, and the repository is much more active than its upsteam counterpart.

It really made my vue map work a lot more painless than rolling my own, which is what I was initially doing.

I hope that helps

like image 174
darryn.ten Avatar answered Oct 13 '22 23:10

darryn.ten


Maybe you should use, or at least study the code, of a package that does that.

For example, you can check vue-google-maps on Github : https://github.com/GuillaumeLeclerc/vue-google-maps/

It defines a whole lot of Components related to Google Maps.

like image 29
Didier Sampaolo Avatar answered Oct 13 '22 23:10

Didier Sampaolo