Monday, January 31, 2022

vue js Page Not Updating in Vue

Foreword: vue-router handover is different from traditional page handover. The switching between routes is actually the switching between components, not the real page switching. This will also lead to a problem, that is, when referring to the same component, it will lead to the component can not be updated, that is, the page in our mouth can not be updated.

I. Problem Presentation

Switching results in routing

At this time, it will be found that the value of the input tag does not change with the change of routing. No updates

II. Solutions

Add a different key value to <router-view:key="key"> </router-view> so that vue recognizes that this is a different <router-view>.

Switching results in routing

At this point, the routing will be updated. But this also means that each < router-view > needs to be bound to a key value. If I jump from page1 to page2 with different components, I really don't have to worry about component updates.

3. Solutions 2

Add a different V-IF value to <router-view v-if="router Alive"> </router-view> to destroy <router-link> and then recreate <router-link> to refresh the page.

Because router-link components have cancel click events, here. native is to trigger events in the component's native tag.

(2) The usage of this. $nextTick (()=> {}) is to wait for this.routerAlive = false; to execute this.routerAlive = true after triggering; thus to destroy and recreate.

Fourth, the extension of solution 2 triggers the refresh of routing within the routing.

Scheme 1, Scheme 2 updates the routing through the outside of the routing. What if you want to update the routing from the inside of the routing?

 1 <!-- App.vue Root Component Code -->
 2 <template>
 3   <div class="app">
 4       <div class="slide">
 5           <ul>
 6               <li><router-link to="/page1/freddy" >freddy</router-link></li>    
 7               <li><router-link to="/page1/nick" >nick</router-link></li>    
 8               <li><router-link to="/page1/mike" >mike</router-link></li>    
 9           </ul>    
10       </div>
11       <div class="content">
12              <router-view v-if="routerAlive"></router-view>
13       </div>
14   </div>
15 </template>
16  
17 <script>
18     export default{
19     data(){
20         return {
21         routerAlive:true
22         }
23     },
24     provide(){    //Create properties in parent components
25             return {
26                 routerRefresh: this.routerRefresh
27             }
28         },
29     methods:{
30         routerRefresh(){
31             this.routerAlive = false;
32         this.$nextTick(()=>{
33             this.routerAlive = true;
34         });
35         }
36     }
37     }
38 </script>
 1 <!-- assembly Page1 Code -->
 2 <template>
 3     <div class="page-1">
 4         Name:<input type="text" v-model="value"><br/>
 5         <button @click="linkToNick1">Jump To nick,No refresh routing</button>
 6         <button @click="linkToNick2">Jump To nick,And refresh routing</button>
 7         <br/>
 8         <button @click="linkToSelf1">Jump to itself,No refresh routing</button>
 9         <button @click="linkToSelf2">Refresh itself</button>
10     </div>
11 </template>
12 <script type="text/javascript">
13     export default {
14     name:'page1',
15     inject:['routerRefresh'],   //Injecting attributes created in parent components into child components
16     mounted(){
17         this.value = this.$route.params.name;
18     },
19     data(){
20         return {
21             value:''
22         }
23     },
24     methods:{
25         linkToNick1(){
26         this.$router.push('/page1/nick');
27         },
28         linkToSelf1(){
29         this.$router.push('/page1/freddy');
30         },
31         linkToNick2(){
32         this.$router.push('/page1/nick');
33         this.routerRefresh();
34         },
35         linkToSelf2(){
36         this.routerRefresh();
37         }
38     }
39     }
40 </script>
41  
42 <style type="text/css">
43     button { margin-top:10px;}
44         button:hover { background:#ff9500; }
45 </style>

(1) When we click "jump to nick, do not refresh the routing", we will find that the value of input has not changed.

(2) When we click "jump to nick and refresh the routing", the value of input has changed.

(3) When we enter some values randomly in the input, and then click "jump to itself, do not refresh the routing", we will find that the contents of the input are not refreshed.

(4) Click refresh itself can trigger refresh routing, is it very practical?

Sunday, January 30, 2022

Update an Object's Property in Array of Objects in JS

 https://bobbyhadz.com/blog/javascript-update-property-of-object-in-array

Change value of object which is inside an array

 https://stackoverflow.com/questions/4689856/how-to-change-value-of-object-which-is-inside-an-array-using-javascript-or-jquer


//Initailize array of objects.
let myArray = [
  {id: 0, name: "Jhon"},
  {id: 1, name: "Sara"},
  {id: 2, name: "Domnic"},
  {id: 3, name: "Bravo"}
],
    
//Find index of specific object using findIndex method.    
objIndex = myArray.findIndex((obj => obj.id == 1));

//Log object to Console.
console.log("Before update: ", myArray[objIndex])

//Update object's name property.
myArray[objIndex].name = "Laila"

//Log object to console again.
console.log("After update: ", myArray[objIndex])

Tuesday, January 25, 2022

How to use a button as a file uploader with Vuetify in Vue.js

<template>
    <div>
        <!-- 1. Create the button that will be clicked to select a file -->
        <v-btn 
            color="primary" 
            rounded 
            dark 
            :loading="isSelecting" 
            @click="handleFileImport"
        >
            Upload File
        </v-btn>

        <!-- Create a File Input that will be hidden but triggered with JavaScript -->
        <input 
            ref="uploader" 
            class="d-none" 
            type="file" 
            @change="onFileChanged"
        >
    </div>
</template>

<script>
    export default {
        name: 'Example',
        data(){
            return {
                isSelecting: false,
                selectedFile: null
            }
        },
        methods: {
            handleFileImport() {
                this.isSelecting = true;

                // After obtaining the focus when closing the FilePicker, return the button state to normal
                window.addEventListener('focus', () => {
                    this.isSelecting = false
                }, { once: true });
                
                // Trigger click on the FileInput
                this.$refs.uploader.click();
            },
            onFileChanged(e) {
                this.selectedFile = e.target.files[0];

                // Do whatever you need with the file, liek reading it with FileReader
            },
        }
    } 

</script>


https://ourcodeworld.com/articles/read/1424/how-to-use-a-button-as-a-file-uploader-with-vuetify-in-vuejs 



Tuesday, January 11, 2022

My pc configarations

Mother board Name: Gigabyte H310M DS2 8th Gen Micro ATX Motherboard

Key Features

Model: H310M DS2

Chipset: Intel H310 Express

Intel Socket 1151 for 8th Gen Core Processors

2 x DIMM, Max. 32GB DDR4

3 x PCIe slots

Link: https://www.startech.com.bd/gigabyte-h310m-ds2-8th-gen-motherboard

Link2: https://www.gigabyte.com/Motherboard/H310M-DS2-20-rev-10#kf 

Processor Name: Intel 9th Gen Core i5 9400F Processor

Key Features

Model: Intel Core i5-9400F

Socket Supported FCLGA1151

Speed 2.90 up to 4.10 GHz

Cores- 6 & Threads- 6

9M Cache

Link: https://www.startech.com.bd/intel-core-i5-9400f-processor

Ram: Corsair Vengeance LPX 4GB (1x4GB) DDR4 DRAM 2400MHz

Key Features

MPN: CMK4GX4M1A2400C16

Model: Corsair Vengeance LPX 4GB

Frequency: 2400 MHz

Operating voltage: 1.2 V

Type: Pin 288

Latency: 14-16-16-31

Description

Gigabyte GT 1030 2GB OC Graphics card

Features

Powered by GeForce® GT 1030 

Integrated with 2GB GDDR5 64bit memory 

Supports HDMI 4K@60Hz 

Smooth 4K video playback and HTML5 web browsing

One-click overclocking via AORUS Graphics Engine

Core Clock

Boost: 1544 MHz/ Base: 1290 MHz in OC Mode

Boost: 1518 MHz/ Base: 1265 MHz in Gaming Mode

02 Years Warranty

Link: https://www.startech.com.bd/gigabyte-ggt-1030-oc-2g

Graphics Card Name : Gigabyte GT 1030 2GB OC Graphics card.

Memory Size : 2GB

Graphics Engine: GeForce® GT 1030

Core Clock: Boost: 1544 MHz/ Base: 1290 MHz in OC Mode   Boost: 1518 MHz/ Base: 1265 MHz in Gaming Mode

Memory Type: GDDR5

Memory Clock: 6008 MHz

Warranty: 2 Years

CasingValue-Top VT-76-L ATX Gaming Casing

SPECIFICATIONS:

CASING

Model: Value Top VT-76-L

Case Type: Mid Tower

Power Supply: 200W

Front Audio Port: 1 x Audio, 1 x Microphone

Cooling Fan (Built-In): 2

Cooling Fan (Optional): 2

Front USB port: 1 x USB3.0, 1 x USB2.0

3.5" Drive Bay: 3

5.25" Drive Bay: 1

Specialty: ATX Gaming Casing

Part No: VT-76-L

Made in/ Assemble: China

Country Of Origin: China

CASE

Port: Front USB port: 1 x USB3.0, 1 x USB2.0

Link: https://www.bdshop.com/value-top-vt-76-l

Monitor One: LG 32ML600M 32" IPS Full HD HDR 75Hz Gaming Monitor

Key Features

  • Model: LG 32ML600M
  • Resolution: 1920 x 1080
  • Response Time: 5ms
  • Viewing Angle: 178° x 178°
  • Brightness: 240cd/m2

Link: https://www.startech.com.bd/lg-32ml600m-gaming-monitor


Monitor Two:  Samsung S19F350HNW 18.5 Inch LED Monitor (VGA)

Key Features

    • Model: Samsung S19F350 18.5''
    • Resolution:1366 X 768, 60 Hz
    • Response Time: 14ms
    • Viewing Angle:170°(H)/160°(V)
    • Contrast Ratio:1000:1

Link: https://www.startech.com.bd/samsung-s19f350-18.5-led-monitor

Friday, January 7, 2022

Laravel clone query

Reuse or clone query()

Typically, we need to query multiple time from a filtered query. So, most of the time we use query() method,

let's write a query for getting today created active and inactive products

$query = Product::query();


$today = request()->q_date ?? today();
if($today){
    $query->where('created_at', $today);
}

// lets get active and inactive products
$active_products = $query->where('status', 1)->get(); // this line modified the $query object variable
$inactive_products = $query->where('status', 0)->get(); // so here we will not find any inactive products

But, after getting $active products the$querywill be modified. So, $inactive_products will not find any inactive products from $query and that will return blank collection every time. Cause, that will try to find inactive products from $active_products ($query will return active products only).

For solve this issue, we can query multiple time by reusing this $query object. So, We need to clone this $query before doing any $query modification action.   



$active_products = (clone $query)->where('status', 1)->get();
  // it will not modify the $query


$inactive_products = (clone $query)->where('status', 0)->get(); 
// so we will get inactive products from $query