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

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...

MySQL 8.0.18 installation tutorial under Windows (illustration)

Download Download address: https://dev.mysql.com/...

Nginx http health check configuration process analysis

Passive Check With passive health checks, NGINX a...

Tudou.com front-end overview

1. Division of labor and process <br />At T...

Detailed discussion on the issue of mysqldump data export

1. An error (1064) is reported when using mysqldu...

Detailed explanation of using echarts map in angular

Table of contents Initialization of echart app-ba...

About the garbled problem caused by HTML encoding

Today a junior student asked a question. The HTML...

Complete step record of vue encapsulation TabBar component

Table of contents Implementation ideas: Step 1: C...

How to install and modify the initial password of mysql5.7.18

For Centos installation of MySQL, please refer to...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

CSS stacking and z-index example code

Cascading and Cascading Levels HTML elements are ...

Implementing password box verification information based on JavaScript

This article example shares the specific code of ...

Detailed explanation of the basic usage of the Linux debugger GDB

Table of contents 1. Overview 2. gdb debugging 2....

About scroll bar in HTML/removing scroll bar

1. The color of the scroll bar under xhtml In the ...