Vue local component data sharing Vue.observable() usage

Vue local component data sharing Vue.observable() usage

As components become more detailed, you will encounter situations where multiple components share state. Vuex can certainly solve this type of problem, but as the official Vuex documentation says, if the application is not large enough, it is best not to use it to avoid cumbersome and redundant code. Today we will introduce the newly added Observable API in vue.js 2.6. By using this api, we can deal with some simple cross-component data state sharing situations.

Creating a store object

First, create a store.js, which contains a store and a mutation, which are used to point to data and processing methods respectively.

//store.js
import Vue from 'vue';

export let store =Vue.observable({count:0,name:'李四'});
export let mutations={
    setCount(count){
        store.count=count;
    },
    changeName(name){
        store.name=name;
    }
}

Apply store objects to different components

Then use the object in your component

//obserVable.vue
<template>
  <div>
    <h1>Cross-component data state sharing observable</h1>
    <div>
      <top></top>
      <bottom></bottom>
    </div>
  </div>
</template>

<script>
import top from './components/top.vue';
import bottom from './components/bottom.vue';
export default {
  name: 'obserVable',
  components:
    top,
    bottom
  }
};
</script>

<style scoped>
</style>
//Component a
<template>
  <div class="bk">
    <span
      ><h1>a component</h1>
      {{ count }}--{{ name }}
    >
    <button @click="setCount(count + 1)">+1 in the current a component</button>
    <button @click="setCount(count - 1)">Current a component -1</button>
  </div>
</template>
<script>
import { store, mutations } from '@/store';
export default {
  computed: {
    count() {
      return store.count;
    },
    name() {
      return store.name;
    }
  },
  methods: {
    setCount: mutations.setCount,
    changeName: mutations.changeName
  }
};
</script>
<style scoped>
.bk {
  background: lightpink;
}
</style>
//Component b
<template>
  <div class="bk">
    <h1>b component</h1>
    {{ count }}--{{ name }}
    <button @click="setCount(count + 1)">+1 in current b component</button>
    <button @click="setCount(count - 1)">Current b component -1</button>
  </div>
</template>
<script>
import { store, mutations } from '@/store';
export default {
  computed: {
    count() {
      return store.count;
    },
    name() {
      return store.name;
    }
  },
  methods: {
    setCount: mutations.setCount,
    changeName: mutations.changeName
  }
};
</script>
<style scoped>
.bk {
  background: lightgreen;
}
</style>

Display Effect

insert image description here

This concludes this article on the use of Vue.observable() for Vue local component data sharing. For more relevant Vue.observable() data sharing content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Let's learn about Vue's life cycle
  • Detailed explanation of Vue3 life cycle hook function
  • A brief discussion on the life cycle of Vue
  • Detailed explanation of data sharing between Vue components
  • Vue implements data sharing and modification operations between two components
  • Detailed explanation of Vue life cycle and data sharing

<<:  MySQL Advanced Learning Notes (Part 3): Introduction to MySQL Logical Architecture, Detailed Explanation of MySQL Storage Engine

>>:  CentOS 6 Compile and install ZLMediaKit analysis

Recommend

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...

How to simulate network packet loss and delay in Linux

netem and tc: netem is a network simulation modul...

border-radius is a method for adding rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

js array fill() filling method

Table of contents 1. fill() syntax 2. Use of fill...

Tutorial on upgrading from Centos7 to Centos8 (with pictures and text)

If you upgrade in a formal environment, please ba...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

CSS example code for implementing sliding doors

The so-called sliding door technology means that ...

How to use html table (to show the visual effect of web page)

We know that when using HTML on NetEase Blog, we ...

How to create, start, and stop a Docker container

1. A container is an independently running applic...

Example analysis of mysql variable usage [system variables, user variables]

This article uses examples to illustrate the usag...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...

How to build php-nginx-alpine image from scratch in Docker

Although I have run some projects in Docker envir...

HTML hyperlink style (four different states) setting example

Copy code The code is as follows: <style type=...