The simplest application of store in Vue is global storage. I use two components here to jump to each other ( First you need to install vuex: Because you need to jump, you have to install the router: Create a new store folder and create a new modules folder, Create Put our variable msg in mystate: const state = { msg: 'This is my status', } export default { state } Getters hold the key-value pairs of the variables we operate on: const getters = { msg:state => state.mystate.msg, } export default getters Index is used to configure and create import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // It will automatically require all vuex modules in your module file // you do not need `import app from './modules/app'` // it will auto require all vuex modules from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters, }) export default store You need to call store and router in the vue instance of import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // It will automatically require all vuex modules in your module file // you do not need `import app from './modules/app'` // it will auto require all vuex modules from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters, }) export default store Configure two routes in import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // It will automatically require all vuex modules in your module file // you do not need `import app from './modules/app'` // it will auto require all vuex modules from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters, }) export default store Use the route view in App.vue: <template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'App' } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style> Finally, there are two page components, Two-way binding and monitoring of msg, putting the new value of msg into the global variable. The method for monitoring triggering is setstate; <template> <div class="hello"> <img src="../assets/logo.png" /> <br /> <input v-model="msg"/> <h2>{{ msg }}</h2> <router-link to="/two">I want to go to the second page</router-link> </div> </template> <script> export default { name: "HelloWorld", data() { return { msg: "Welcome to new vue project", }; }, methods: { setstate(value) { sessionStorage.setItem('msg', value); }, }, watch: msg(newName, oldName) { this.setstate(newName); }, }, }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> a { color: #42b983; } </style> In the second page component <template> <div> This is the second page<h2>{{ msg }}</h2> <router-link to="/">I want to go back</router-link> </div> </template> <script> export default { data() { return { msg: "", }; }, methods: { setmsg() { this.msg = sessionStorage.getItem('msg'); }, }, created(){ this.setmsg() } }; </script> Directory structure: Demo: Initial state: To change the content of the input box: Go to the second page: SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Steps to build MHA architecture deployment in MySQL
>>: Nginx Service Quick Start Tutorial
Summarize Global environment ➡️ window Normal fun...
There are many read-write separation architecture...
Stored Functions What is a stored function: It en...
Table of contents mysql log files binlog Binlog l...
The default storage directory of mysql is /var/li...
When joining a Windows 2008 server subdomain to a...
What are XHTML tags? XHTML tag elements are the b...
1. COUNT(*) and COUNT(COL) COUNT(*) usually perfo...
MySQL 5.7 adds many new features, such as: Online...
environment: 1. Windows Server 2016 Datacenter 64...
The complete syntax of the select statement is: S...
Implementation effect: 1. count(1) and count(*) W...
What is MIME TYPE? 1. First, we need to understand...
In HTML, the Chinese phrase “學好好學” can be express...
Preface For a data-centric application, the quali...