Monday, November 1, 2021

vue js: How to Communicate between Components in Vue.js

 Lnk

Every framework has its own unique architecture but they have one thing in common. The entire page is divided into small components where the component has its own functionalities and UI. And sometimes, we need a way to communicate between these components.

Generally, we use Vuex (State management tool) to store the data and use it across components. But sometimes, we need a way to send the data from one component to another without using the Vuex store. In this article, we will learn the possible ways of achieving that.

There are five ways to send the data from one component to another:

1. Using Props (Parent to Child Communication)

2. Using Events (Child to Parent Communication)

3. Using Event Bus (Communication between any components)

4. Using provide/inject (Parent to Child Communication)

5. Using this.$refs (Parent to Child Communication)

Let’s study them one by one and see how they work.

1) Using Props (Communication from parent to child )

One can send data from parent-to-child components using props. In the below example, we are sending the data from parent. vue to child. vue via the prop message.

Child Component

We have created a component that prints the message. We have defined a variable in props called message which we are rendering in the template.

Parent Component

While we nest the child component inside our parent component, we are binding the data we want to send by using the directive v-bind and sending the data using props.

Any time the parent changes the prop, the new value is sent to the child and rerendered.

2) Using Events (Communication from child to parent )

This is similar to props but we are sending the data from child to parent and instead of using v-bind, we are using directive v-on for capturing.

We have defined a variable in the child component which contains our message. We are sending the same to the parent component by firing an event from the child component.

this.$emit(’name-of-the-event’, args1, args2, args3,...) is a way to fire the event. This is captured on the parent component using directive v-on. Note that one should use the same event name they sent while capturing and can retrieve the arguments.

Child Component


Parent Component

Any time the message in the child component changes, the new value is sent to the parent and rerendered.

3) Using Event Bus (Communication between any two components)

An event bus is used to communicate between any two components (Components need not have a parent-child relationship). This can be used when one needs to manually listen for events on a component instance.

You can just send the data from one component using this.$root.$emit(’name-of-emitter’, args1, args2, ...) and is captured using the same name like this this.$root.$on(’name-of-emitter’, args1, args2, ...) in the other component.

Child Component

Note that we are using mounted() {} to capture the event in component-two. vue.

Parent Component

4) Using Provide/Inject (Parent to child communication)

This is used in the structure where you have deeply nested components and you only need something from the parent component in the deeply nested child.

In that case, you still need to pass the prop down the whole component chain which might be annoying. For such cases, we can use the provide and inject pair.

Although the example we have here is very simple, we are still sending a message from parent to child and hence it works.

We are using our parent component as a provider, sending the data, and injecting it into the child component using the same name.

Child Component

Parent components can serve as dependency providers for all its children, regardless of how deep the component hierarchy is.

5) Using this.$refs (Parent to Child Communication)

This method is the least preferred way of making the communication but it is one of the options available.
Despite the other methods, sometimes you might still need to directly access a child component. To achieve this you can assign a reference ID to the child component using the ref attribute.

Parent Component

Here we are referencing the child component as ‘child’ (ref= child) (reference ID) and then capturing it using this.$refs.child. We can access anything related to the child component in this way.

ChildComponent

Conclusion:

And that sums it up. Comment below if you know other ways of communication between components in Vue.js. Thank you.


Tuesday, October 19, 2021

kShop docker project installer commands

 

kShop project has to three folders

1.    Project (App)

2.    kshop-measurement-api (Api)(python)

3.    kShop (php) this file save the mesurment in database

  1. Project (App)

      The app which will use the API for mesurment.

  2. kshop-measurement-api

    This file holds the measurement calculation code which is api based.

kshop-measurement-api is made fully dockerised. So to run this api project your system must need docker setup.

To run this API go to kshop-measurement-api folder and open wsl command prompt if you are in windows OS.

Make Docker image and run project in single command:

                 docker-compose up -d --build

After this command kshop-measurement-api will serve in

http://localhost:5000/ 

Later command which you may needs after docker installation:

§  To see all docker images press: docker images

§  To see all containers : docker container ls

§  To stop all Docker containers, simply run the following command in your terminal::

1.     docker kill $(docker ps -q)

2.     docker rm $(docker ps -a -q)

3.     docker rmi $(docker images -q)

 

3. kShop

This project is fully made up with php for storing measurement data which was sended by python kshop-measurement-api 

Place this project in htdocs or www folder so that kshop-measurement-api  can use this. 

Note: kshop-measurement-api  project file __init__.py line 212  res = requests.post('http://192.168.1.198/kshop/api/measurement/create_measurement.php', data=data)

You needs to modify your localhosthost IP. (E.g. http://192.168.1.198)

 

This project has a database name kshop. So to use this project import the database also.

 

             kShop Api Test from postman

url: http://localhost:5000/upload

set : header as below
x-api-key:eiWee8ep9due4deeshoa8Peichai8Eih

 

set: body params as like below

image1: file.jpg

image2:file2.jpg

imga3:file3.jpg

customer_id:11

customer_gender:male

customer_age:22

customer_height:164

customer_weight:86

Thursday, October 14, 2021

Install Docker Desktop on Windows and Linux

 

For Windows System requirements:

1.     Enable wsl2 feature in your windows.

2.     Enable virtualization to you system. (Docker desktop needs this feature)

3.     Download and install Docker Desktop

 

1. Enable wsl2 feature

WSL 2 (Windows subsystem for Linux) backend because Docker must run in Linux environment for this you have to enable wsl2 in your windows.

Enable the WSL 2 feature on Windows. For detailed instructions, refer to the (https://docs.microsoft.com/en-us/windows/wsl/install).

The following hardware prerequisites are required to successfully run WSL 2 on Windows 10 or Windows 11:

·         For Windows 11 64-bit: Home or Pro version 21H2 or higher, or Enterprise or Education version 21H2 or higher.

·         For Windows 10 64-bit: Home or Pro 2004 (build 19041) or higher, or Enterprise or Education 1909 (build 18363) or higher.

·         4GB system RAM

After you successfully install and setup wsl2 you will get wsl command prompt as like windows command prompt form search menu or any other place. With this terminal you can place Linux command in windows.

 

2. Enable virtualization

BIOS-level hardware virtualization support must be enabled in the BIOS settings. For detail info read this info: https://docs.docker.com/desktop/windows/troubleshoot/#virtualization-must-be-enabled

 

 

If your bios level virtualization is turned off then you have to turn it on.

To see virtualization is enable or not go to task manager by pressing ctrl+shif+esc button then go to performance tab.

See this article: https://mashtips.com/enable-virtualization-windows-10/

 

3. Download and install Docker Desktop

Download from here: (https://docs.docker.com/desktop/windows/install/)


                                                     For Linux

Click here to see details

Wednesday, October 6, 2021

vs-code configaration settings

 {

  "editor.fontSize": 17,

  "better-comments.multilineComments": true,

  "better-comments.highlightPlainText": false,

  "yaml.schemas": {

    "file:///c%3A/Users/Mehedi/.vscode/extensions/atlassian.atlascode-2.8.3/resources/schemas/pipelines-schema.json": "bitbucket-pipelines.yml"

  },

  "atlascode.bitbucket.enabled": false,

  "javascript.implicitProjectConfig.checkJs": false,

  "javascript.implicitProjectConfig.experimentalDecorators": false,

  // "files.autoSave": "afterDelay",

  "workbench.sideBar.location": "left",

  "window.zoomLevel": 1,

  "sync.autoDownload": true,

  "sync.autoUpload": true,

  "sync.gist": "a315796d6cea0099a0339668499857a8",

  "git.enableSmartCommit": true,

  "phpfmt.psr1": false,

  // "phpfmt.exclude": [

  //   "AllmanStyleBraces"

  // ],

  "phpfmt.passes": [

    "AlignDoubleArrow",

    "AlignEquals",

    "AlignDoubleSlashComments",

    "AlignGroupDoubleArrow",

    "AlignPHPCode",

    "AutoSemicolon",

    "ShortArray"

  ],

  "editor.formatOnSave": false,

  "[php]": {

    "editor.defaultFormatter": "kokororin.vscode-phpfmt",

    "editor.formatOnSave": true

  },

  "phpfmt.enable_auto_align": true,

  "workbench.iconTheme": "vscode-icons",

  "highlight-matching-tag.styles": {

    "opening": {

      "name": {

        "underline": "yellow"

      },

      "left": {

        "custom": {

          "borderWidth": "0 0 0 1px",

          "borderStyle": "dotted",

          "borderColor": "yellow",

          "borderRadius": "5px",

          "overviewRulerColor": "white"

        }

      },

      "right": {

        "custom": {

          "borderWidth": "0 1px 0 0",

          "borderStyle": "dotted",

          "borderColor": "yellow",

          "borderRadius": "5px",

          "overviewRulerColor": "white"

        }

      },

    }

  },

  "highlight-matching-tag.noDefaultEmptyElements": true,

  "[vue]": {

    "editor.defaultFormatter": "esbenp.prettier-vscode",

    "editor.formatOnSave": true

  },

  "[javascript]": {

    "editor.formatOnSave": true,

    "editor.defaultFormatter": "vscode.typescript-language-features",

  },

  "vetur.format.defaultFormatter.js": "vscode-typescript",

  "vetur.format.options.tabSize": 4,

  "workbench.colorTheme": "Monokai Night",

  "vsicons.dontShowNewVersionMessage": true,

  "tabnine.experimentalAutoImports": true,

  "workbench.editor.untitled.hint": "hidden",

  "files.exclude": {

    "": true

  },

  "search.useIgnoreFiles": false,

}

Tuesday, September 28, 2021

laravel 8 data_fill

 $data = [

    'products' => [

        ['name' => 'Desk 1', 'price' => 100],

        ['name' => 'Desk 2'],

    ],

];


data_fill($data, 'products.*.price', 200);


/*

    [

        'products' => [

            ['name' => 'Desk 1', 'price' => 100],

            ['name' => 'Desk 2', 'price' => 200],

        ],

    ]

*/

laravel difference between fill and update

<?php

$user = User::find(1);

// This will update immediately

$user->update(['first_name' => 'Braj', 'last_name' => 'Mohan']);


//But what if you do not want to update immediately. Suppose you also want to make user active before the actual update. Then fill method comes handy.

$user = User::find(1);

// This will not update underlying data store immediately

$user->fill(['first_name' => 'Braj', 'last_name' => 'Mohan']);

// At this point user object is still only in memory with updated values but actual update query is not performed.

// so we can have more logic here 

$user->is_active = true;

// Then finally we can save it.

$user->save(); // This will also make user active


//Update method is dumb and makes the query to database even if no values are changed. But save method is intelligent and calculates if you really changed anything and do not perform query if nothing has changed for example.


$user = User::find(1);

//suppose user object has following values after fetching

// first_name = 'Braj';

// second_name = 'Mohan';

// is_active = true;

// Then if you set the values like following without any change

$user->is_active = true; // it is already true so no change

$user->save(); // this won't perform any database query hence is efficient